JavaScript Puzzlers! At the beginning thought is just a few small questions, the results do doubt life, to suspect that I may learn javascript in vain. Readers can try it.

Without further ado, just to the point:

The first question



["1"."2"."3"].map(parseInt)Copy the code

Let’s first look at array. map:



array.map(function(currentValue,index,arr).thisValue)Copy the code

The map() method processes the elements in order of the original array elements, returning a new array with the values of the original array elements processed by the call.

Map takes two arguments, a callback function and an optional callback function’s this value, which defaults to undefined

The callback takes the value of the current element, the optional index value of the current element, and the array object to which the current element belongs

Let’s look at parseInt again:



parseInt(string, radix)Copy the code

The parseInt() function parses a string and returns an integer. String: required. String to be parsed. Radix: optional. Represents the radix of the number to be parsed. The value is between 2 and 36.

When ignoring parameter radix, the cardinality of JavaScript default numbers is as follows:

  • If a string begins with “0x”, parseInt() parses the rest of the string into a hexadecimal integer.

  • If a string begins with 0, ECMAScript V3 allows an implementation of parseInt() to parse the following character into an octal or hexadecimal number.

  • If a string begins with a number from 1 to 9, parseInt() will parse it as a decimal integer.

To break it down, it’s:



// parseInt(currenValue, index)
parseInt('1'.0)    / / 1
parseInt('2'.1)    // NaN
parseInt('3'.2)    // NaNCopy the code

The answer is: [1, NaN, NaN]

The second question



[typeof null.null instanceof Object]Copy the code

Typeof returns a string representing a type. The instanceof operator is used to check whether constructor. Prototype exists on the prototype chain of the argument object.

We all know that in javascript everything is an Object, so the first part goes without saying that the Object must be returned, but null does not exist on the prototype chain of the parameter Object.

So the answer is: [Object, false]

The third question



[[3.2.1].reduce(Math.pow), [].reduce(Math.pow) ]Copy the code

Let’s start with array. reduce:



array.reduce(function(total, currentValue, currentIndex, arr).initialValue)Copy the code

Reduce takes two arguments, a callback function and an optional initial value that will be used to accumulate.

If initialValue is provided, the reduce method calls callbackFN once (in ascending index order) for each element in the array. If initialValue is not provided, the reduce method calls the callbackfn function for each element starting with the second.

The callback function takes four arguments, in order

  • The value obtained from the last call to the callback function. If initialValue is provided to the Reduce method, total is initialValue when the function is called for the first time.

  • The value of the current array element.

  • The numeric index of the current array element.

  • The array object that contains the element.



Math.pow(x,y)Copy the code

The pow() method returns x to the y power.

[3, 2, 1].reduce(math.pow)



Math.pow(3.2)    / / 9
Math.pow(9.1)    / / 9Copy the code

But Reduce throws an exception in two cases:

  • TypeError is raised if any of the following conditions are met: the CallbackFN parameter is not a function object.

  • The array contains no elements and no initialValue is provided.

So [].reduce(math.pow) throws an exception

A. an error B. an error C. an error D. an error

The fourth question



var val = 'smtg';
console.log('Value is ' + (val === 'smtg')?'Something' : 'Nothing');Copy the code

This looks easy, right? The answer is “Value is Something.” If you really think that, you will cry. Yes, I have tried the poison myself.

So basically, the priority of + is greater than that of?

So the original question is equivalent to:



console.log('Value is true' ? 'Something' : 'Nothing')Copy the code

The answer should be Something

The fifth problem



var name = 'World! ';
(function (a) {
    if (typeof name === 'undefined') {
        var name = 'Jack';
        console.log('Goodbye ' + name);
    } else {
        console.log('Hello ' + name);
    }
})();Copy the code

“Goodbye, Jack,” I said

Why? Because in javascript, declared variables or functions are promoted, that is, variable promotion is the action of javascript to move the declaration to the top of the scope (global or current function scope). You can use a function or variable first and then declare it:



show('Wiess')    // Hello Wiess
function (name) {
    console.log('Hello ' + name)
}Copy the code

But javascript only promotes declarations, not initializations, and if you use a value that is declared and initialized after it has already been used, the value will be undefined

So this is equivalent to:



var name = 'World! ';
(function (a) {
    var name;
    if (typeof name === 'undefined') {
        name = 'Jack';
        console.log('Goodbye ' + name);
    } else {
        console.log('Hello ' + name);
    }
})();Copy the code

Do you have a little liver pain here? Get used to it. Get used to it

The sixth question



var END = Math.pow(2.53);
var START = END - 100;
var count = 0;
for (var i = START; i <= END; i++) {
    count++;
}
console.log(count);
Copy the code

Math.pow(2, 53) = 9007199254740992(the maximum that can be expressed) The maximum value plus 1 is still 9007199254740992, so the cycle keeps going

The answer is: other

Number 7



var ary = [0.1.2];
ary[10] = 10;
ary.filter(function(x) { return x === undefined; });Copy the code

Array.filter():



var newArray = array.filter(function(currentValue,index,arr).thisValue)Copy the code

The filter() method creates a new array of elements by checking all eligible elements in the specified array.

Filter accepts two arguments, one for the callback function and the this value of the optional callback function, which defaults to undefined

The callback function takes three arguments in turn:

  • Must be. The value of the current element

  • Optional. The index value of the current element

  • Optional. The array object to which the current element belongs

And sparse matrices, when you take some undefined number in the array:



arr[4]    // undefinedCopy the code

But when you walk through it, you see that it has no elements. JavaScript skips these gaps.

[]

The eighth problem



var two   = 0.2
var one   = 0.1
var eight = 0.8
var six   = 0.6
[two - one == one, eight - six == two]Copy the code

[false, false] [true, false]

Let’s not mention that javascript doesn’t have exact floating-point numbers. Everyone knows that, but why not? ! I wish there was a god to answer it

Question 9



function showCase(value) {
    switch(value) {
    case 'A':
        console.log('Case A');
        break;
    case 'B':
        console.log('Case B');
        break;
    case undefined:
        console.log('undefined');
        break;
    default:
        console.log('Do not know! ');
    }
}
showCase(new String('A'));Copy the code

A new String(‘A’) is not the same as A literal declaration of ‘A’, so Do not know!

The first ten questions



function showCase2(value) {
    switch(value) {
    case 'A':
        console.log('Case A');
        break;
    case 'B':
        console.log('Case B');
        break;
    case undefined:
        console.log('undefined');
        break;
    default:
        console.log('Do not know! ');
    }
}
showCase2(String('A'));Copy the code

String (x) does not create an object, but returns a String, i.e. typeof String (1) === “String”

A. Case B. Case C. Case D. Case