-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01_Basics.js
More file actions
141 lines (103 loc) · 3.75 KB
/
Copy path01_Basics.js
File metadata and controls
141 lines (103 loc) · 3.75 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
// This is a simple JavaScript file that prints "Hello World!" to the console.
console.log("Hello World!");
// JavaScript is a Dynamically typed language, which means you don't need to declare the type of a variable.
Name = "Ritik";
age= 21;
isTrue = true;
// This is an alert box that will display a message to the user.
alert("Hello, this is an alert!");
// Prompt is used to take input from the user.
const userName = prompt("Please enter your name:");
console.log("Hello, " + userName + "!");
// Data types in JavaScript :-
// Primitive Data Types :-
// Number, String, Boolean, Undefined, Null, Symbol, BigInt
console.log(typeof Name); // Output: string
console.log(typeof age); // Output: number
console.log(typeof isTrue); // Output: boolean
console.log(typeof undefined); // Output: undefined
console.log(typeof null); // Output: object
console.log(typeof Symbol()); // Output: symbol
console.log(typeof BigInt(100)); // Output: bigint
// Object Data Types :-
// Object, Array, Function, Date, RegExp, Error, Map, WeakMap, Set, WeakSet
const person = {
name: "Ritik",
age: 21,
isStudent: true,
hobbies: ["Reading", "Coding", "Sports"],
address: {
city: "New Delhi",
country: "India",
pincode: 110001
}
};
console.log(person); // Output: { name: 'Ritik', age: 21, isStudent: true, hobbies: [ 'Reading', 'Coding', 'Sports' ], address: { city: 'New Delhi', country: 'India', pincode: 110001 } }
console.log(typeof person); // Output: object
// Operators in JavaScript :-
// Arithmetic Operators
console.log(1 + 1); // Output: 2
console.log(5 - 3); // Output: 2
console.log(4 * 5); // Output: 20
console.log(10 / 2); // Output: 5
console.log(10 % 3); // Output: 1
// Assignment Operators
let x = 5;
x += 3; // Equivalent to x = x + 3;
console.log(x); // Output: 8
let y = 5;
y -= 3; // Equivalent to y = y - 3;
console.log(y); // Output: 2
let z = 5;
z *= 3; // Equivalent to z = z * 3;
console.log(z); // Output: 15
let a = 5;
a /= 3; // Equivalent to a = a / 3;
console.log(a); // Output: 1.6666666666666667
let b = 5;
b %= 3; // Equivalent to b = b % 3;
console.log(b); // Output: 2
// Comparison Operators
console.log(5 > 3); // Output: true
console.log(5 < 3); // Output: false
console.log(5 >= 3); // Output: true
console.log(5 <= 3); // Output: false
console.log(5 == "5"); // Output: true (loose equality)
console.log(5 === "5"); // Output: false (strict equality)
// Equality Operators
console.log(5 == 5); // Output: true
console.log(5 === 5); // Output: true
console.log(5 != 5); // Output: false
console.log(5 !== 5); // Output: false
// Logical Operators
console.log(true && false); // Output: false
console.log(true || false); // Output: true
// Bitwise Operators
console.log(~5); // Output: -6
// Example: 5 in binary is 0101, so ~5 is 1010 in binary, which is -6 in decimal.
console.log(5 & 3); // Output: 1
// Example: 5 in binary is 0101 and 3 in binary is 0011, so 5 & 3 is 0001 in binary, which is 1 in decimal.
console.log(5 | 3); // Output: 7
// Example: 5 in binary is 0101 and 3 in binary is 0011, so 5 | 3 is 0111 in binary, which is 7 in decimal.
console.log(5 ^ 3); // Output: 6
// Example: 5 in binary is 0101 and 3 in binary is 0011, so 5 ^ 3 is 0110 in binary, which is 6 in decimal.
// Assignment Operators
x = 5;
x <<= 3; // Equivalent to x = x << 3;
console.log(x); // Output: 40
let y = 5;
y >>= 3; // Equivalent to y = y >> 3;
console.log(y); // Output: 1
let z = 5;
z >>>= 3; // Equivalent to z = z >>> 3;
console.log(z); // Output: 1
// Increment and Decrement Operators
let a = 5;
a++; // Equivalent to a = a + 1;
console.log(a); // Output: 6
let b = 5;
b--; // Equivalent to b = b - 1;
console.log(b); // Output: 4
// Ternary Operator
const isStudent = true;
console.log(isStudent ? "Yes" : "No"); // Output: Yes