-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtut5.js
More file actions
83 lines (63 loc) · 2 KB
/
tut5.js
File metadata and controls
83 lines (63 loc) · 2 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
76
77
78
79
80
81
82
83
// Type Conversion
// Ex-1
let i;
i = 32;
console.log(i, (typeof i));
i = String(32);
console.log(i, (typeof i));
// Ex-2
booleanVar = true;
console.log(booleanVar, (typeof booleanVar));
booleanVar = String(true);
console.log(booleanVar, (typeof booleanVar));
// Ex-3
let date = new Date();
console.log(date, (typeof date));
let date2 = String(new Date());
console.log(date2, (typeof date2));
//Ex-4
let arr = [54, 76, 32, 98, 9];
console.log(arr, (typeof arr));
console.log("Length of array ", arr.length, (typeof arr));
let arr1 = String([54, 76, 32, 98, 9]);
console.log(arr1, (typeof arr1));
console.log("Length of string ", arr1.length, (typeof arr1));
// Type conversion to string using toString:
// Ex-5
let myVar = 5;
console.log(myVar.toString());
//Ex-6
let date3 = new Date();
d = date3.toString()
console.log(d, (typeof d));
// Converting to number:
//Ex-7
let x = "32";
x1 = Number(x);
console.log(x1, (typeof x1));
//Ex-7
let y = "32a";
y1 = Number(y);
console.log(y1, (typeof y1)); // it will return NaN (not a number)
//Ex-8
let isStaff = true; // Remember true = 1 , false = 0
console.log(isStaff, (typeof isStaff));
console.log(Number(isStaff), (typeof isStaff));
// convert to number using parseInt (parseInt will remove decimal part of number)
let num1 = '78.95';
console.log(num1, typeof(num1));
num2 = parseInt(num1);
console.log(num2, typeof(num2));
// convert to number using parseFloat (parseFloat will show decimal part of number)
num3 = parseFloat(num1);
console.log(num3, typeof(num3));
// set decimal place using toFixex (toFixed is used to set decimal place with numbers)
age = 46
console.log(age, typeof(age));
console.log(age.toFixed(), typeof(age)); // no decimal place
console.log(age.toFixed(2), typeof(age)); // give 2 decimal place
// Type Coercion
let val1 = "234"; // string
let val2 = 56; // number
console.log(val1+val2); // it will concatenate 23456
console.log(Number(val1)+val2); // it will sum 290