Note:arrays are by default coerced to strings by simply joining all the values with commas (,) in between. You might think that two arrays with the same contents would be == equal, but they're not:
var a = [1,2,3];var b = [1,2,3];var c = "1,2,3";
a == c; // trueb == c; // truea == b; // falseb === c; // false
var a = [1,2,3];var b = [1,2,3];var c = "1,2,3";
a == c; // trueb == c; // truea == b; // falseb === c; // false
۱۵۵
۱۴:۱۷
Inequality
if both values in the < comparison are strings, as it is with b < c, the comparison is made lexicographically (aka alphabetically like a dictionary). But if one or both is not a string, as it is with a < b, then both values are coerced to be numbers, and a typical numeric comparison occurs.var a = 41;var b = "42";var c = "43";
a < b; // trueb < c; // true
Note:
var a = 42;
var b = "foo";
a < b; // false
a > b; // false
a == b; // false
b value is being coerced to the "invalid number value" NaN in the < and > comparisons, and the specification says that NaN is neither greater-than nor less-than any other value
if both values in the < comparison are strings, as it is with b < c, the comparison is made lexicographically (aka alphabetically like a dictionary). But if one or both is not a string, as it is with a < b, then both values are coerced to be numbers, and a typical numeric comparison occurs.var a = 41;var b = "42";var c = "43";
a < b; // trueb < c; // true
Note:
var a = 42;
var b = "foo";
a < b; // false
a > b; // false
a == b; // false
b value is being coerced to the "invalid number value" NaN in the < and > comparisons, and the specification says that NaN is neither greater-than nor less-than any other value
۱۵۷
۱۴:۲۰
۱۵۴
۱۴:۳۰
In JavaScript, variable names (including function names) must be valid identifiers.An identifier must start with a-z, A-Z, $, or _. It can then contain any of those characters plus the numerals 0-9.However, certain words cannot be used as variables, but are OK as property names. These words are called "reserved words," and include the JS keywords (for, in, if, etc.) as well as null, true, and false.
۱۵۶
۱۲:۰۷
۱۵۶
۱۲:۰۸
Hoisting
Wherever a var appears inside a scope, that declaration is taken to belong to the entire scope and accessible everywhere throughout.Metaphorically, this behavior is called hoisting,Consider:
var a = 2;
foo(); // works because `foo()` // declaration is "hoisted"
function foo() { a = 3;
console.log( a ); // 3
var a; // declaration is "hoisted" // to the top of `foo()`}
console.log( a ); // 2
Warning: It's not common or a good idea to rely on variable hoisting to use a variable earlier in its scope than its var declaration appears; it can be quite confusing
Wherever a var appears inside a scope, that declaration is taken to belong to the entire scope and accessible everywhere throughout.Metaphorically, this behavior is called hoisting,Consider:
var a = 2;
foo(); // works because `foo()` // declaration is "hoisted"
function foo() { a = 3;
console.log( a ); // 3
var a; // declaration is "hoisted" // to the top of `foo()`}
console.log( a ); // 2
Warning: It's not common or a good idea to rely on variable hoisting to use a variable earlier in its scope than its var declaration appears; it can be quite confusing
۱۵۶
۱۲:۱۱
In addition to creating declarations for variables at the function level, ES6 lets you declare variables to belong to individual blocks (pairs of { .. }), using the let keyword.function foo() { var a = 1;
if (a >= 1) { let b = 2;
while (b < 5) { let c = b * 2; b++;
console.log( a + c ); } }}
foo();// 5 7 9
Because of using let instead of var, b will belong only to the if statement and thus not to the whole foo() function's scope. Similarly, c belongs only to the while loop. Block scoping is very useful for managing your variable scopes in a more fine-grained fashion, which can make your code much easier to maintain over time.
if (a >= 1) { let b = 2;
while (b < 5) { let c = b * 2; b++;
console.log( a + c ); } }}
foo();// 5 7 9
Because of using let instead of var, b will belong only to the if statement and thus not to the whole foo() function's scope. Similarly, c belongs only to the while loop. Block scoping is very useful for managing your variable scopes in a more fine-grained fashion, which can make your code much easier to maintain over time.
۱۵۹
۱۲:۲۰
۱۵۸
۱۲:۳۲
if..else..if statements like this:if (a == 2) { // do something}else if (a == 10) { // do another thing}else if (a == 42) { // do yet another thing}else { // fallback to here}
switch statement:switch (a) { case 2: // do something break; case 10: // do another thing break; case 42: // do yet another thing break; default: // fallback to here}
. If you omit break from a case, and that case matches or runs, execution will continue with the next case's statements regardless of that case matching.
switch (a) { case 2: case 10: // some cool stuff break; case 42: // other stuff break; default: // fallback}Here, if a is either 2 or 10, it will execute the "some cool stuff" code statements.
var a = 42;
var b = (a > 41) ? "hello" : "world";
switch statement:switch (a) { case 2: // do something break; case 10: // do another thing break; case 42: // do yet another thing break; default: // fallback to here}
. If you omit break from a case, and that case matches or runs, execution will continue with the next case's statements regardless of that case matching.
switch (a) { case 2: case 10: // some cool stuff break; case 42: // other stuff break; default: // fallback}Here, if a is either 2 or 10, it will execute the "some cool stuff" code statements.
var a = 42;
var b = (a > 41) ? "hello" : "world";
۱۷۸
۱۲:۳۶
کتاب جاوا اسکریپت
In JavaScript, variable names (including function names) must be valid identifiers. An identifier must start with a-z, A-Z, $, or _. It can then contain any of those characters plus the numerals 0-9. However, certain words cannot be used as variables, but are OK as property names. These words are called "reserved words," and include the JS keywords (for, in, if, etc.) as well as null, true, and false.
Keywords reserved for future JavaScript versions can NOT be used as variable names in strict mode.
These are:
implementsinterfaceletpackageprivateprotectedpublicstaticyield
These are:
implementsinterfaceletpackageprivateprotectedpublicstaticyield
۱۷۹
۷:۰۵