📜 ⬆️ ⬇️

Quiz for those who love javascript more ECMAscript

Quiz for those who love javascript more ECMAScript. Main prize: patty on the shelf.

Update: Explanation. For those who are not at all in the subject. ECMAScript is the language standard on which JavaScript implementations in modern browsers are based. It is this standard that determines the behavior of programs in this article. JavaScript is an add-on over ECMAScript.

Explanation number 2. IE glitches are not directly related to the quiz because it is an error (or “feature”) of the standards implementation.
')


* Questions:

Question 1. What will the next program display?

var a = 123;

function foo() {
alert(typeof a);
var a = '123';
}

foo();


Question 2. What will the next program display?

alert(sum1(3, 4));
alert(sum2(3, 4));

function sum1(a, b) { return a + b; }
var sum2 = function(a, b) { return a + b; }


Question 3. What will the next program display?

function foo(){
return
{
code: 1
};
}

alert(foo());


Question 4. What will the next program display?

alert([1,2,].length + [,1,2].length);


Last question. A bit from another area.

Question 5. How to speed up the execution of the calc function?

var Multiplier = 123; // Global

function calc() { var s = 0; for (var i = 0; i < arguments.length; i++) s += Multiplier * arguments[i]; return s; }


* Right answers:

Answer 1. "undefined"

Answer 2. First, "7", then an error.

Answer 3. "undefined"

Answer 4. "5"

Answer 5. function calc() { var s = 0, m = Multiplier; for (var i = arguments.length; i--; ) s += m * arguments[i]; return s; } function calc() { var s = 0, m = Multiplier; for (var i = arguments.length; i--; ) s += m * arguments[i]; return s; }

* Correct answers with explanation:

Answer 1. "undefined"

When a variable is declared, it is placed in the scope that corresponds to the function in which it is declared. If a variable is declared outside of functions, it is placed in the global scope. Creation of a variable occurs when control is received by a function with its declaration. Or program if variable is global. When you create a variable in ECMAScript, it becomes undefined. If a variable is declared with initialization, initialization does not occur at the time the variable is created, but when the string with the var instruction is executed.

Answer 2. First, "7", then an error.

The most significant difference between defining a function using declarations (sum1) and defining a function using an expression (sum2) is that the declarations are carried out before the code runs, and the expression as it executes.

Answer 3. "undefined"

The specification declares the autocompletion of lines with semicolons, which means that if there is a line break, the instruction before the transfer can be equipped with this sign. Here, the string c “return” contains a valid instruction in the language and, since a newline follows, the autocompletion of the lines is triggered by semicolons.

Answer 4. "5"

The trailing comma when specifying an array is discarded without adding an empty element to the array.
Update for loving IE. IE incorrectly inserts an empty element at the end of such an array.

Answer 5.

function calc() { var s = 0, m = Multiplier; for (var i = arguments.length; i--; ) s += m * arguments[i]; return s; }

For most JavaScript implementations, two considerations are true: access to local variables is faster, the “for (var i = arguments.length; i--;)” loop contains fewer instructions than the usual one.

* Links:

ru.wikipedia.org/wiki/ECMAScript

Thanks for attention.

PS Removed the white color of the text. Whoever reads this blog does not need it.
Thank you for the article. / terloger /

Source: https://habr.com/ru/post/81571/


All Articles