01 - Types - Values - Variables
01 - Types - Values - Variables
1 2
1 2
3 4
1
21/02/2024
5 6
5 6
▸A JavaScript identifier must begin with a letter, an underscore (_), or a debugger default delete do
dollar sign ($). Subsequent characters can be letters, digits, underscores, double else enum* eval
export* extends* false final
or dollar signs.
finally float for function
▸***Digits are not allowed as the first character in JavaScript. goto if implements import*
in instanceof int interface
▸Example
let* long native new
null package private protected
public return short static
super* switch synchronized this
throw throws transient true
try typeof var void
volatile while with yield
7 8
7 8
2
21/02/2024
9 10
9 10
11 13
11 13
3
21/02/2024
14 15
14 15
16 17
16 17
4
21/02/2024
18 19
18 19
20 21
20 21
5
21/02/2024
22 23
24 25
6
21/02/2024
26 27
28 29
28 29
7
21/02/2024
Variables Variables
Declarations with let and const Variable Declarations with var
▸Variables declaration and assignment values ▸In versions of JavaScript before ES6, the only way to declare a variable is
let i; // Value = undefined let message = "hello"; with the var keyword, and there is no way to declare constants.
let sum; let i = 0, j = 0, k = 0; - The syntax of var is just like the syntax of let
let i, sum; let x = 2, y = x*x; // Initializers can use previously declared variables
var x;
var data = [], count = data.length;
▸It is a good programming practice to assign an initial value to your
variables when you declare them.
▸To declare a constant instead of a variable, use const instead of let ▸it is legal to declare the same variable multiple times with var.
const H0 = 74; // Hubble constant (km/s/Mpc) //If you re-declare a JavaScript variable declared with var, it will not lose its value.
const C = 299792.458; // Speed of light in a vacuum (km/s) var carName = "Volvo";
const AU = 1.496E8; // Astronomical Unit: distance to the sun (km) var carName; //let will cause with error
print(carName) //==>Volvo
30 31
30 31
Variables Summary
Destructuring Assignment
▸How to write and manipulate numbers and strings of text in JavaScript.
▸In a destructuring assignment, the value on the righthand side of the
equals sign is an array or object (a “structured” value), and the lefthand ▸How to work with JavaScript’s other primitive types: booleans, Symbols,
side specifies one or more variable names using a syntax that mimics array
and object literal syntax. null, and undefined.
let [x, y] = [1, 2]; // Same as let x=1, y=2 let [x, y] = [1]; // x == 1; y == undefined ▸The differences between immutable primitive types and mutable
[x, y] = [x + 1, y + 1]; // Same as x = x + 1, y = y + 1 [x, y] = [1, 2, 3]; // x == 1; y == 2
reference types.
[x, y] = [y, x]; // Swap the value of the two variables [, x, , y] = [1, 2, 3, 4]; // x == 2; y == 4
[x, y] // => [3,2]: the incremented and swapped values let [x, ...y] = [1, 2, 3, 4]; // x=1, y == [2,3,4]
▸How JavaScript converts values implicitly from one type to another and
▸Destructuring assignment works with functions that return arrays of values how you can do so explicitly in your programs.
let foo = () => {
return [1, 2];
▸How to declare and initialize constants and variables (including with
}
destructuring assignment) and the lexical scope of the variables and
Read more at MDN web docs
let [x, y] = foo(); constants you declare.
print(x + ", " + y); //1, 2
32 33
32 33
8
21/02/2024
Exercise
1. https://www.geeksforgeeks.org/practice-javascript-online/
2. https://www.w3schools.com/js/exercise_js.asp
3. …
34 35
34 35