forked from learning-zone/javascript-basics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatatypes.html
75 lines (64 loc) · 2.02 KB
/
datatypes.html
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
<!DOCTYPE html>
<html>
<head>
<title>DataTypes in JS</title>
</head>
<script>
/**
* Six data types that are primitives:
* Boolean
* Null
* Undefined
* Number
* String
* Symbol (new in ECMAScript 6):- “Symbol” value represents a unique identifier.
* and Object
*
**/
// ---------------
// Dynamic typing
// ---------------
var val = 10; console.log("type of val: "+typeof(val)); // Number
val = 10.20; console.log("type of val: "+typeof(val)); // Number
val = 'Hello World'; console.log("type of val: "+typeof(val)); // String
val = true; console.log("type of val: "+typeof(val)); // Boolean
val = null; console.log("type of val: "+typeof(val)); // Object
val = undefined; console.log("type of val: "+typeof(val)); // Undefined
var obj = new Object(); console.log("type of obj: "+typeof(obj)); // Undefined
var n; console.log("type of n: "+typeof(n)); // null datatype
console.log("type of x: "+typeof(x)); // null datatype
// --------------
// let vs var
// --------------
function varTest() {
var x = 1;
if (true) {
var x = 2; // same variable!
console.log("var: Inside value of x: "+x); // 2
}
console.log("var: Outside value of x: "+x); // 2
}
varTest();
function letTest() {
let x = 1;
if (true) {
let x = 2; // different variable
console.log("let: Inside value of x: "+x); // 2
}
console.log("let: Outside value of x: "+x); // 1
}
letTest();
// ----------
// Symbols
// ----------
// id is a new symbol
let id = Symbol(); console.log("Type of id: "+typeof(id));
// id is a symbol with the description "id"
let id1 = Symbol("id");
let id2 = Symbol("id");
console.log("Is id1 == id2: "+(id1 == id2));
</script>
<body>
DataTypes in JavaScript
</body>
</html>