JavaScript types and variables

JavaScript is not a typed language, since types are not declared and variables can receive different types, but data values do have types. The main types are Number, String, Boolean, Array, Object, and Function. The first three are scalar types, and the last three are also objects. A value is treated differently in the same expression if it has one type or another. For example, in an expression such as a = b + c, the value of a will be different if b and c are numbers (they will be added) or if one of them is a string (they will be concatenated).

Values can be compared, and their types are important if the comparison is strict (for example, using === instead of ==). But it can be confusing to rely on such conversions (0, "", null, NaN , and undefined are all considered false, but the  false string converts to true, since its not an empty string).

In ES5 JavaScript, var was the only keyword for declaring a variable. It ignores block scope and is hoisted to the top of the functions. Since ES6 (ES2015), two new keywords have been introduced: const and let. They both are block-scoped and need to be assigned a value before they are used (var defaults to undefined). Declarations with const are constants and can't be reassigned. It's usually considered good practice to use const whenever possible, and only use let if you actually need to redefine a variable.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset