Tutorial by Examples

You can't reassign constants. const foo = "bar"; foo = "hello"; Prints: Uncaught TypeError: Assignment to constant.
Declaring a variable const only prevents its value from being replaced by a new value. const does not put any restrictions on the internal state of an object. The following example shows that a value of a property of a const object can be changed, and even new properties can be added, because the ob...
You can initialize a constant by using the const keyword. const foo = 100; const bar = false; const person = { name: "John" }; const fun = function () = { /* ... */ }; const arrowFun = () => /* ... */ ; Important You must declare and initialize a constant in the same statement....
There are four principle ways to declare a variable in JavaScript: using the var, let or const keywords, or without a keyword at all ("bare" declaration). The method used determines the resulting scope of the variable, or reassignability in the case of const. The var keyword creates a f...
JavaScript variables can hold many data types: numbers, strings, arrays, objects and more: // Number var length = 16; // String var message = "Hello, World!"; // Array var carNames = ['Chevrolet', 'Nissan', 'BMW']; // Object var person = { firstName: "John", ...
Declared variable without a value will have the value undefined var a; console.log(a); // logs: undefined Trying to retrieve the value of undeclared variables results in a ReferenceError. However, both the type of undeclared and unitialized variables is "undefined": var a; console...
To assign a value to a previously declared variable, use the assignment operator, =: a = 6; b = "Foo"; As an alternative to independent declaration and assignment, it is possible to perform both steps in one statement: var a = 6; let b = "Foo"; It is in this syntax that...
Increment by var a = 9, b = 3; b += a; b will now be 12 This is functionally the same as b = b + a; Decrement by var a = 9, b = 3; b -= a; b will now be 6 This is functionally the same as b = b - a; Multiply by var a = 5, b = 3; b *= a; b will now...

Page 1 of 1