Conversions
let score = "hitesh"
console.log(typeof score);
console.log(typeof(score));
let valueInNumber = Number(score)
console.log(typeof valueInNumber);
console.log(valueInNumber);
Console Output:
string
string
number
NaN
101
Conversion into Number
"33" => 33"33abc" => NaN
true => 1; false => 0
let a = null
console.log(Number(a))
Console Output: 0
Conversion into Boolean
let isLoggedIn = "hitesh"
let booleanIsLoggedIn = Boolean(isLoggedIn)
console.log(booleanIsLoggedIn);
Console Output:
true
"Aakash" => True
0 => False
1 => True
Conversion into String
let someNumber = 33
let stringNumber = String(someNumber)
console.log(stringNumber);
console.log(typeof stringNumber);
Console Output:
true
String
JavaScript Arithmetic Operators
Arithmetic operators are used to perform arithmetic between variables and/or values.
Given that y = 5
, the table below explains the arithmetic operators:
Operator | Description | Example | Result |
---|---|---|---|
+ | Addition | x = y + 2 |
x = 7 |
- | Subtraction | x = y - 2 |
x = 3 |
* | Multiplication | x = y * 2 |
x = 10 |
/ | Division | x = y / 2 |
x = 2.5 |
% | Modulus (division remainder) | x = y % 2 |
x = 1 |
++ | Increment | x = ++y |
x = 6 |
-- | Decrement | x = --y |
x = 4 |
Assignment operators are used to assign values to JavaScript variables.
Given that x = 10
and y = 5
, the table below explains the assignment operators:
Operator | Example | Same As | Result |
---|---|---|---|
= | x = y |
x = 5 |
|
+= | x += y |
x = x + y |
x = 15 |
-= | x -= y |
x = x - y |
x = 5 |
*= | x *= y |
x = x * y |
x = 50 |
/= | x /= y |
x = x / y |
x = 2 |
%= | x %= y |
x = x % y |
x = 0 |
Some Operations
let value = 3
let negValue = -value
let numCheck = negValue + 5
console.log(numCheck);
console.log(negValue);
Console Output:
2
-3
console.log("1" + 2);
console.log(1 + "2");
console.log("1" + 2 + 2);
console.log(1 + 2 + "2");
Console Output:
12
12
122
32
console.log(+true);
console.log(+"");
Console Output:
1
0
let gameCounter = 100
++gameCounter;
console.log(gameCounter);
Console Output:
101
Link to dive more deep