-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvariable-value.js
69 lines (60 loc) · 1.83 KB
/
variable-value.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
// Will output undefined
console.log(myVar);
var myVar = "Hello";
console.log(myVar); // Hello
// Will output error: myVar2 is not defined
// console.log(myVar2);
// How in the world can I call a function before its declared?
// Because of hoisting
test();
function test() {
var myVar3;
console.log(myVar3);
console.log(c)
var c = {
name: "Moi"
}
}
/*
- JavaScriptissä undefined on yksi primitiivisistä tyypeistä: number, string, boolean, undefined, jne.
- undefined arvo on oletusarvo kaikille muuttujille JavaScriptissä
- undefined on seurausta hoistingista
- ennen kuin js-koodia aletaan suorittamaan rivi riviltä läpi,
JavaScript engine lukee koodin läpi ja tallentaa muistiin funktio määritykset ja muuttujat.
Funktiot tallennetaan muistiin kokonaisuudessaan
Muuttujille ei aseteta arvoa (esim. myVar = "Hello" rivi suoritetaan vasta suoritusvaiheessa)
Eli muuttujille tulee ikään kuin placeholderina undefined arvo
*/
/**
* ES6-part
* - var variables are function scoped
* - let/const variables are {}-block scoped
* - var variables gets undefined default value, let/const does not
*/
if (true) {
var varName = "Sauli";
}
// let variable
if (true) {
let letName = "Sauli";
}
console.log(varName);
// console.log(letName); // Uncaught ReferenceError: letName is not defined
// console.log(constName); // Real error, no more undefined 🎉
// const constName;
// let, if value change. const, if not
// Tip: always const, let if needed
const age = 19;
// age = 20; // Uncaught TypeError: Assignment to constant variable.
// You can change value of object item even in const
// Even if JS allows this, do not do it
const sale = {
name: "Sale",
age: 34
};
sale.age = 25;
// // Uncaught TypeError: Assignment to constant variable.
// sale = {
// name: "Sale",
// age: 25,
// }