-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatatypes.js
210 lines (139 loc) · 7.34 KB
/
datatypes.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
DATA TYPES
JavaScript has primitive data types and object types.
Following are the primitive data types:
- null
- undefined
- boolean
- number
- bigint
- string
- symbol
Following are object types:
- array
- function
- object
1) The Undefined Type
The undefined type is a primitive type that has only one value undefined. By default, when a variable is declared but not initialized, it is assigned the value of undefined.
Consider the following example:
let counter;
console.log(counter); // undefined
console.log(typeof counter); // undefined
2) THE NULL DATATYPE
The null type is the second primitive data type that also has only one value null.
In JavaScript, null is "nothing". It is supposed to be something that doesn't exist. Unfortunately, in JavaScript, the data type of null is an object. You can consider it a bug in JavaScript that typeof null is an object. A proposal to fix this was proposed but rejected. The reason was that the fix would break a lot of existing sites.
let obj = null;
console.log(typeof obj); // object
3) THE NUMBER TYPE
JavaScript uses the number type to represent both integer and floating-point numbers.
The following statement declares a variable and initializes its value with an integer:
let num = 100;
To represent a floating-point number, you include a decimal point followed by at least one number. For example:
let price = 12.5;
let discount = 0.05;
console.log(price + " " + discount);
4) NaN DATA TYPE
NaN stands for Not a Number. It is a special numeric value that indicates an invalid number. For example, the division of a string by a number returns NaN:
console.log('a' / 2); // NaN;
The NaN has two special characteristics:
1 > Any operation with NaN returns NaN.
2 > The NaN does not equal any value, including itself.
Here are some examples:
console.log(NaN / 2); // NaN
console.log(NaN == NaN); // false
5) THE STRING TYPE
In JavaScript, a string is a sequence of zero or more characters. A string literal begins and ends with either a single quote(') or a double quote (").
JavaScript strings are primitive values. Also, strings are immutable. It means that if you modify a string, you will always get a new string. The original string doesn’t change.
A string that begins with a double quote must end with a double quote. Likewise, a string that begins with a single quote must also end with a single quote:
let greeting = 'Hi';
let message = "Bye";
The template literals allow you to use the single quotes and double quotes inside a string without the need for escaping them. For example:
let mesage = \`"I'm good". She said"\`;
console.log(mesage);
5) The boolean type
The boolean type has two literal values: true and false in lowercase. The following example declares two variables that hold the boolean values.
let inProgress = true;
let completed = false;
let x = 5 > 4;
console.log(typeof completed); // boolean
console.log(x);
6) THE SYMBOL TYPE
JavaScript added a primitive type in ES6: the symbol. Different from other primitive types, the symbol type does not have a literal form.
To create a symbol, you call the Symbol function as follows:
let s1 = Symbol("s1");
console.log(s1);
Uses of symbol
Using symbols as unique values
Whenever you use a string or a number in your code, you should use symbols instead. For example, you have to manage the status in the task management application.
Before ES6, you would use strings such as open, in progress, completed, canceled, and on hold to represent different statuses of a task. In ES6, you can use symbols as follows:
let statuses = {
OPEN: Symbol('Open'),
IN_PROGRESS: Symbol('In progress'),
COMPLETED: Symbol('Completed'),
HOLD: Symbol('On hold'),
CANCELED: Symbol('Canceled')
};
console.log(statuses);
7) JavaScript BigInt
ES2020 introduced a new built -in object called BigInt that allows you to represent whole numbers larger 253 - 1.
The bigint is the primitive type like number, string, symbol, boolean undefined, and null.
To make a BigInt, you append n to the end of the number literal, for example:
let bigInt = 9007199254740991n;
Alternatively, you can call the function BigInt():
let bigInt = BigInt(9007199254740991);
JAVASCRIPT OBJECT TYPE
There are three common object types used in JS:
1) Object
2) Function
3) Array
1) Object
In JavaScript, an object is an unordered collection of key - value pairs. Each key - value pair is called a property.
The key of a property can be a string. And the value of a property can be any value, e.g., a string, a number, an array, and even a function.
JavaScript provides you with many ways to create an object. The most commonly used one is to use the object literal notation.
The following example creates an empty object using the object literal notation:
let empty = {};
To create an object with properties, you use the key:value within the curly braces. For example, the following creates a new person object:
let person = {
firstName: 'John',
lastName: 'Doe'
};
Accessing properties
To access a property of an object, you use one of two notations: the dot notation and array - like notation.
1) The dot notation
console.log(person.firstName);
2) Array - like notation([])
The following illustrates how to access the value of an object’s property via the array - like notation:
console.log(person['firstName']);
Introduction to JavaScript arrays
In JavaScript, an array is an ordered list of values. Each value is called an element specified by an index:
A JavaScript array has the following characteristics:
First, an array can hold values of mixed types. For example, you can have an array that stores elements with the types number, string, boolean, and null.
Second, the size of an array is dynamic and auto - growing. In other words, you don’t need to specify the array size up front.
Creating JavaScript arrays
JavaScript provides you with two ways to create an array. The first one is to use the Array constructor as follows:
let scores = new Array(10, 20);
The more preferred way to create an array is to use the array literal notation:
let arrayName = [element1, element2, element3, ...];
Accessing JavaScript array elements
JavaScript arrays are zero - based indexed. In other words, the first element of an array starts at index 0, the second element starts at index 1, and so on.
To access an element in an array, you specify an index in the square brackets:
arrayName[index]
for example:
primitive vs reference
stack and heap memory
When you declare variables, the JavaScript engine allocates memory for them in two memory locations: stack and heap.
Static data is the data whose size is fixed at compile time. Static data includes:
- Primitive values (null, undefined, boolean, number, string, symbol, and BigInt)
- Reference values that refer to objects.
Because static data has a size that does not change, the JavaScript engine allocates a fixed amount of memory space to the static data and stores it on the stack.
For example, the following declares two variables and initializes their values to a literal string and a number:
let name = 'John';
let age = 25;
Unlike the stack, JavaScript stores objects (and functions) on the heap. The JavaScript engine doesn’t allocate a fixed amount of memory for these objects. Instead, it allocates more space as needed.
The following example defines the name, age, and person variables:
let name = 'John';
let age = 25;
let person = {
name: 'John',
age: 25,
};