-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconst.js
78 lines (59 loc) · 1.49 KB
/
const.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
* Introduction to the JavaScript const keyword
* const CONSTANT_NAME = value;
*/
let x = 10;
x = 20;
x = x + 10;
console.log(x); // 30
const RATE = 0.8;
RATE = 0.9; // TypeError: Assignment to constant variable.
// SyntaxError: Missing initializer in const declaration
const RED;
/**
* JavaScript const and Objects
*/
const person = {
age : 20
};
person.age = 25;
console.log(person.age); // 25
person = { age: 50 }; // TypeError: Assignment to constant variable.
const user = Object.freeze({
age: 55
});
user.age = 60; // TypeError
const company = Object.freeze({
name: 'Google',
address: {
street: '1600 Amphitheatre Pkwy',
city: 'Mountain View',
state: 'CA',
zip: 94043
}
});
/**
* JavaScript const and Arrays
*/
const colors = ['red'];
colors.push('blue');
console.log(colors); // ['red', 'blue']
colors.pop();
colors.pop();
console.log(colors); // []
colors = []; // TypeError: Assignment to constant variable.
/**
* JavaScript const in a for loop
*/
let scores = [90, 85, 80, 75, 70];
// If you intend to modify the score variable inside the loop, you must use let
for(let score of scores) {
console.log(score); // 90 85 80 75 70
}
// If you don't intend to modify the score variable inside the loop, you can use the const keyword instead of let
for(const score of scores) {
console.log(score); // 90 85 80 75 70
}
for(const i = 0; i < scores.length; i++) {
console.log(scores[i]); // Error: Assignment to constant variable.
}