-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path04 -JavaScriptOperators.js
More file actions
100 lines (76 loc) · 2.31 KB
/
04 -JavaScriptOperators.js
File metadata and controls
100 lines (76 loc) · 2.31 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
// Arithmetic operators
//Arithmetic operators are symbols that represent mathematical operations.
let a = 10;
let b = 20;
let c = a+b;
console.log(c);
let userInput = prompt("Enter a number ")
let number = parseint(userInput);
if(number % 2 === 0){
console.log(`${number} is even number`);
} else {
console.log(`${number} is odd number`);
}
// Increment operators
let x = 10;
x = x + 1;//Same as x++ and x = x + 1;
x++;
console.log(x)
// Decrement
let y = 20;
y = y - 1;
y--;
console.log(y);
//Predators precedent
let result = 10 + 5 * 2;
let result = 10 + 10; // 5 * 2 evaluates to 10
let result = 20; // 10 + 10 evaluates to 20
// Comparison
// JavaScript utilizes comparison operators to compare two values and return a boolean result (true or false).
let p = 40;
let q = 5;
console.log(p == q);
console.log(p === q);
console.log(p !== q);
console.log(p > q);
console.log(p < q);
console.log(p <= q);
console.log(p >= q);
// Logical Operators
// Logical operators are symbols or keywords used to combine conditional statements, resulting in a Boolean (true or false) output.
//&& operators
let a = true;
let b = false;
console.log(a && b); // Output: false
console.log(true && true); // Output: true
//|| OR operators
let x = true;
let y = false;
console.log(x || y); // Output: true
console.log(false || false); // Output: false
// ! NOT operators
let isRaining = true;
console.log(!isRaining); // Output: false
console.log(!false); // Output: true
let value = null;
let defaultValue = "default";
console.log(value ?? defaultValue); // Output: "default"
let zeroValue = 0;
console.log(zeroValue ?? defaultValue); // Output: 0
// Assignment Operators
//JavaScript assignment operators are used to assign values to variables. The most fundamental is the simple assignment operator, =, which assigns the value of its right operand to its left operand.
let h = 10;
let j = 20
h = h + j; //same as "j += k;"
j += k;
j = j - h;
j -= k;
j *= h;
j /= h;
j %= h;
// TERNARY OPERATORS
// The JavaScript ternary operator, also known as the conditional operator, provides a concise way to write conditional expressions. It is the only JavaScript operator that takes three operands.
//EX:
let age = 20;
let message = (age >= 18) ? "You are an adult." : "You are not yet an adult.";
console.log(message); // Output: You are an adult.