-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJS2_datatypes.js
More file actions
152 lines (111 loc) · 4.64 KB
/
Copy pathJS2_datatypes.js
File metadata and controls
152 lines (111 loc) · 4.64 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// 1st type is NUMBER
var myFavnum = 59;
console.log("The number is " + myFavnum);
//2nd type is STRING
var myName = "Yash";
console.log("My name is "+ myName);
//3rd type is BOOLEAN
var isRaining = true;
console.log(isRaining);
//4th type is UNDEFINED
var yourName;
console.log(yourName);
//5th type is NULL
var badMemories = null;
console.log(badMemories);
//6th type is BIGINT
const bigNumber = 222222222222222222222222222222222n;
console.log(bigNumber);
//questiosn for the interview
//1. What is the difference between the Undefined and Null in the javasccript
//2. What is the purpose of typeof operator in javascript
//Lets see this question with the example
//So basically the use of typeof operator is to see what is the data type of value in the variable
var myNewName = "Yash Mashru";
console.log(myNewName);
console.log("The typeof the data defined here is " + typeof(myNewName));
//3. What is the result of 'typeof null' in javascript ?
//So this is the interesting one here, So basically typpeof null shold be null but it will provide as a object
//Lets see this with the example
var badMemories = null;
console.log(badMemories);
console.log(typeof badMemories);
//The typeof null will be object//
//4. What are the primitive data types in JavaScript ?
//As we know there are 7 primitivee data types in the JavaScript and those are as follows;
// String, Number, Bollean, Undefined, Null, bigint, symbol
//5. Convert a string to the number
//We just need to add '+' sign before the string
///lets learn with the example
var newStrig = "10";
console.log(newStrig);
console.log(typeof +newStrig);
console.log(typeof Number(newStrig));
//Number here is the constructor in the JAVASCRIPT
//6. Convert a Number to String
let str = 5;
console.log(str);
console.log(typeof str);
console.log(typeof String(str));
console.log(typeof (str + ""));
//7. Explain the concept of the truthy and falsy values in the javascript
// True
// Aany non empty string, any non-zero number, array, and objects even if they are not empty
//false, 0(Zero value), ''(an empty String), null, undefined, NaN(Not a Number)
// Lets see this one with the example here
//To check if a non-empty string is a truthy or falsy we have written an example
var helloWorld = "Yash";
if(helloWorld){
console.log("This is the true value");
}else{
console.log("This is the false value");
}
//Lets lern about parseInt and parseFloat
//parseInt is the functionwhich is used to convert a string into an integer(i.e Whole number)
const myString1 = "42";
const myNumber1 = parseInt(myString1);
console.log(myNumber1);
const myString2 = "42.5";
const myNumber2 = parseInt(myString2);
console.log(myNumber2);//As we see here the parseInt converts the string into the whole number.
//parseFloat is also Function which is userd to convert the string into the floating number which can contain the decimal numbers also
const myString3 = "42.5";
const myNumber3 = parseFloat(myString3);
console.log(myNumber3);
//Few more examples for the parseInt and the parseFloat
console.log(parseInt("123"));
//This will print 123
console.log(parseInt('123', 10));
//This will also print 123
console.log(parseInt(" 123"));
//Here this will ignore the space before the numbers and outputt will be 123
console.log(parseInt("077"));
console.log(parseFloat("077"));
//Here this will ignore the 0 which is written before the 77 and output will be only 77
console.log(parseInt("1.9"));
console.log(parseFloat("1.9"));
//Here this will conver this number to whole number and give the output as the 1
//When ww will not get an output
//basically here in these examples it'll return as a NaN i.e Not a Number
console.log(parseInt("&123"));
console.log(parseInt("-123"));
console.log(parseInt("xyz"));
//What is the purpose of the NaN in the JavaScript ?
//Let's say we have passed some values into the parseInt that is not
//a number, so there should be something that can say that this is not the number. So that's why NaN is used
//When a mathemetical operation doesn't yeilds a valid number NaN is returned
//Also, to check whether a value is number or not we can use isNaN() function
console.log(isNaN('xyz'));
console.log(isNaN('12'));
// NaN == NaN, why is it false ?
if(NaN == NaN){
console.log("both are qual");
}else{
console.log("not equal");
}
//NaN is not qual to NaN
//Lets see why here
console.log(parseInt("Yash"));
console.log(parseInt("%$&"));
//See here we got the NaN values here but both NaN are from the different Values so that is the reason why
//NaN is Not qual to NaN;