forked from learning-zone/javascript-basics
- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdatatypes.html
More file actions
Latest commit
75 lines (64 loc) · 2.02 KB
/
Copy pathdatatypes.html
File metadata and controls
75 lines (64 loc) · 2.02 KB
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
// ---------------
varval=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
varobj=newObject();console.log("type of obj: "+typeof(obj));// Undefined
varn;console.log("type of n: "+typeof(n));// null datatype
console.log("type of x: "+typeof(x));// null datatype
// --------------
// let vs var
// --------------
functionvarTest(){
varx=1;
if(true){
varx=2;// same variable!
console.log("var: Inside value of x: "+x);// 2
}
console.log("var: Outside value of x: "+x);// 2
}
varTest();
functionletTest(){
letx=1;
if(true){
letx=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
letid=Symbol();console.log("Type of id: "+typeof(id));
// id is a symbol with the description "id"
letid1=Symbol("id");
letid2=Symbol("id");
console.log("Is id1 == id2: "+(id1==id2));
</script>
<body>
DataTypes in JavaScript
</body>
</html>