A quiz for those who love Javascript more than ECMAscript
A quiz for those who love JavaScript more than ECMAScript. The main prize: a pie on a shelf.
Update: Explanation. For those who are completely off topic. ECMAScript is the language standard on which JavaScript implementations in modern browsers are based. It is this standard that defines the behavior of the programs in this article. JavaScript is an "add-on" over ECMAScript.
Explanation number 2. IE glitches are not directly related to the quiz because these are errors (or “features”) of the standards implementation.
* Questions:
Question 1. What will the next program output? Question 2. What will the following program output? Question 3. What will the next program output? Question 4. What will the following program output? Last question. A bit from another area.
Question 5. How to speed up the calc function? * Correct answers: Answer 1. “undefined” Answer 2. First “7”, then an error. Answer 3. “undefined” Answer 4. “5” Answer 5. * Correct answers with an explanation: Answer 1. “undefined”
When declaring a variable, it is placed in the scope corresponding to the function in which it is declared. If a variable is declared outside functions, it is placed in the global scope. Creation of a variable occurs when a function is declared with its declaration. Or a program if the variable is global. When creating a variable in ECMAScript, it becomes undefined. If a variable is declared with initialization, initialization occurs not at the time the variable is created, but when the line with the var instruction is executed.
Answer 2. First “7”, then an error.
The most significant difference between defining a function using an declaration (sum1) and defining a function using an expression (sum2) is that declarations are executed before the code executes, and the expression as it executes.
Answer 3. “undefined”
The specification declares a mechanism for completing strings with semicolons, which leads to the fact that if there is a line break, the instruction can be provided with this sign before the line break. Here, the string “return” contains an instruction valid in the language and, since the next line feed follows, the mechanism for completing strings with semicolons is triggered.
Answer 4. “5” The
trailing comma is discarded when an array is specified without adding an empty element to the array.
Update for those who love IE. IE incorrectly inserts an empty element at the end of such an array.
Answer 5.
For most JavaScript implementations, two considerations are true: local variables are accessed faster, the “for (var i = arguments.length; i--;)” loop contains fewer instructions than regular.
* Links:
ru.wikipedia.org/wiki/ECMAScript
Thank you for your attention.
PS Removed the white color of the text. Those who read this blog do not need it.
Thank you for the article. / terloger /
Update: Explanation. For those who are completely off topic. ECMAScript is the language standard on which JavaScript implementations in modern browsers are based. It is this standard that defines the behavior of the programs in this article. JavaScript is an "add-on" over ECMAScript.
Explanation number 2. IE glitches are not directly related to the quiz because these are errors (or “features”) of the standards implementation.
* Questions:
Question 1. What will the next program output? Question 2. What will the following program output? Question 3. What will the next program output? Question 4. What will the following program output? Last question. A bit from another area.
var a = 123;
function foo() {
alert(typeof a);
var a = '123';
}
foo();
alert(sum1(3, 4));
alert(sum2(3, 4));
function sum1(a, b) { return a + b; }
var sum2 = function(a, b) { return a + b; }
function foo(){
return
{
code: 1
};
}
alert(foo());alert([1,2,].length + [,1,2].length);
Question 5. How to speed up the calc function? * Correct answers: Answer 1. “undefined” Answer 2. First “7”, then an error. Answer 3. “undefined” Answer 4. “5” Answer 5. * Correct answers with an explanation: Answer 1. “undefined”
var Multiplier = 123; // в объекте Global
function calc() { var s = 0; for (var i = 0; i < arguments.length; i++) s += Multiplier * arguments[i]; return s; }function calc() { var s = 0, m = Multiplier; for (var i = arguments.length; i--; ) s += m * arguments[i]; return s; }When declaring a variable, it is placed in the scope corresponding to the function in which it is declared. If a variable is declared outside functions, it is placed in the global scope. Creation of a variable occurs when a function is declared with its declaration. Or a program if the variable is global. When creating a variable in ECMAScript, it becomes undefined. If a variable is declared with initialization, initialization occurs not at the time the variable is created, but when the line with the var instruction is executed.
Answer 2. First “7”, then an error.
The most significant difference between defining a function using an declaration (sum1) and defining a function using an expression (sum2) is that declarations are executed before the code executes, and the expression as it executes.
Answer 3. “undefined”
The specification declares a mechanism for completing strings with semicolons, which leads to the fact that if there is a line break, the instruction can be provided with this sign before the line break. Here, the string “return” contains an instruction valid in the language and, since the next line feed follows, the mechanism for completing strings with semicolons is triggered.
Answer 4. “5” The
trailing comma is discarded when an array is specified without adding an empty element to the array.
Update for those who love 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: local variables are accessed faster, the “for (var i = arguments.length; i--;)” loop contains fewer instructions than regular.
* Links:
ru.wikipedia.org/wiki/ECMAScript
Thank you for your attention.
PS Removed the white color of the text. Those who read this blog do not need it.
Thank you for the article. / terloger /