بله | کانال کتاب جاوا اسکریپت
ک

کتاب جاوا اسکریپت

۳۸ عضو
The following built-in types are available:
stringnumberbooleannull and undefinedobjectsymbol (new to ES6)JavaScript provides a typeof operator that can examine a value and tell you what type it is:
var a;typeof a; // "undefined"
a = "hello world";typeof a; // "string"
a = 42;typeof a; // "number"
a = true;typeof a; // "boolean"
a = null;typeof a; // "object" -- weird, bug
a = undefined;typeof a; // "undefined"
a = { b: "c" };typeof a; // "object"The return value from the typeof operator is always one of six (seven as of ES6! - the "symbol" type) string values. That is, typeof "abc" returns "string", not string.

۱۲:۲۰

undefined Objects:The object type refers to a compound value where you can set properties (named locations) that each hold their own values of any type.
var obj = { a: "hello world", b: 42, c: true};
obj.a; // "hello world"obj.b; // 42
obj["a"]; // "hello world"obj["b"]; // 42
Properties can either be accessed with dot notation (i.e., obj.a) or bracket notation (i.e., obj["a"]).

۱۲:۲۳

The [ ] notation requires either a variable (explained next) or a string literal (which needs to be wrapped in " .. " or ' .. ').
Of course, bracket notation is also useful if you want to access a property/key but the name is stored in another variable, such as:
var obj = { a: "hello world", b: 42};
var b = "a";
obj[b]; // "hello world"obj["b"]; // 42

۱۲:۲۶

Array && function : array and fvar arr = [ "hello world", 42, true];
arr[0]; // "hello world"arr[1]; // 42arr[2]; // truearr.length; // 3
typeof arr; // "object"unction is object.
function foo() { return 42;}
foo.bar = "hello world";
typeof foo; // "function"typeof foo(); // "number"typeof foo.bar; // "string"function is a main type -- and can thus have properties, but you typically will only use function object properties (like foo.bar) in limited cases.

۱۲:۲۹

undefined Built-In Type Methods

۱۲:۳۰

var a = "hello world";var b = 3.14159;
a.length; // 11a.toUpperCase(); // "HELLO WORLD"b.toFixed(4); // "3.1416"

JS automatically "boxes" the value to its object wrapper counterpart (hidden under the covers).
A string value can be wrapped by a String object, a number can be wrapped by a Number object, and a boolean can be wrapped by a Boolean object.

۱۲:۵۱

undefined Comparing Values

۱۲:۵۲

CoercionIf you have a number but need to print it on the screen, you need to convert the value to a string, and in JavaScript this conversion is called "coercion."

Coercion comes in two forms in JavaScript: explicit and implicit. Explicit coercion is simply that you can see obviously from the code that a conversion from one type to another will occur, whereas implicit coercion is when the type conversion can happen as more of a non-obvious side effect of some other operation
Here's an example of explicit coercion:
var a = "42";
var b = Number( a );
a; // "42"b; // 42 -- the number!And here's an example of implicit coercion:
var a = "42";
var b = a * 1; // "42" implicitly coerced to 42 here
a; // "42"b;

۱۳:۰۰

Truthy & Falsy
The specific list of "falsy" values in JavaScript is as follows:
"" (empty string)0, -0, NaN (invalid number)null, undefinedfalseAny value that's not on this "falsy" list is "truthy."

۱۳:۱۰

Equality
The proper way to characterize them is that == checks for value equality with coercion allowed, and === checks for value equality without allowing coercion; === is often called "strict equality" for this reason.I believe == is a powerful tool that helps your program, if you take the time to learn how it works.


here are my simple rules:If either value (aka side) in a comparison could be the true or false value, avoid == , != and use ===, !==.If either value in a comparison could be one of these specific values (0, "", or [] -- empty array), avoid ==, != and use === , !==.

۱۳:۲۷

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

۱۴:۱۷

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

۱۴:۲۰

undefined Variables

۱۴:۳۰

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.

۱۲:۰۷

undefined Function Scopes:

۱۲:۰۸

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

۱۲:۱۱

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.

۱۲:۲۰

undefined Conditionals

۱۲:۳۲

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";

۱۲:۳۶

کتاب جاوا اسکریپت
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

۷:۰۵