Variables in JavaScript
variables are containers used to store data values that can be manipulated or referenced later in a program
- let - A variable declared with let can be changed later.(Block-Scoped)
- const - A variable declared with const cannot be reassigned(Block-Scoped).
- var - var was the first way to declare variables in JavaScript. It was used before let and const were introduced.One thing to remember is that var can also be redeclared(Global-Scoped).
Operators
Operators are special symbols or keywords used to perform calculations, compare values, or manipulate data.
1.Arithmetic Operators
Used to perform standard mathematical equations.
Addition(+): Adds numbers or links strings together.
Subtraction(-): Finds the difference between numbers. Multiplication(): Multiplies numbers.
Division(/): Divides Numbers to find the quotient.
Modulo(%): Returns the leftover division remainder.
Exponentiation(*): Raises a number to a power.
2.Assignment Operators
Used to store or update values inside variables.
Assignment(=): Assigns a value to a variable.
Addition Assignment(+=): Adds a value to a variable and updates it.
Subtraction Assignment(-=): Subtracts a value and updates the variable.
Multiplication Assignment(*=): Multiplies a variable and updates it.
Division Assignment(/=): Divides a variable and updates it
3.Comparison Operators
These compare two values and always return a boolean true or false
Equality(==): Checks if values match, ignoring their data type.
Strict Equality(===): Checks if both the values and data types match.
Inequality(!=): Checks if values are not equal.
Strict Inequality(!==): Checks if values or types do not match. Greater Than(>): Returns true if the left value is bigger.
Less Than(<): Returns true if the left value is smaller.
Greater Than or Equal(>=): Checks if the left value is bigger or equal.
Less Than or Equal(<=): Checks if the left value is smaller or equal.
4.Logical Operators
Used to combine multiple conditions or reverse boolean values.
AND(&&): Returns true if both conditions are true.
OR(||): Returns true if at least one condition is true.
NOT(!): Reverses a boolean value (true to false)
5.Unary Operators
Operators that require only a single value to work.
Increment(++): Increases a numeric value by 1.
Two types of Increment PostIncrement(i++) and PreIncrement(--i)
Decrement(--): Decreases a numeric value by 1.
Two types of Decrement PostDecrement(i++) and PreDecrement(--i)
Important
In addition operator only concatenation will happen when merge two different data types and in other operators we will try to merge different data types it will show "NaN"(Not-a-Number)
Addition
Eg: 10+"5" = 105;
"Sam"+2 = Sam2
Subtraction
Eg: 10-"2" = 8
"Sam"-2 = NaN // Here you can see a difference that in subtraction when try to merge the difference data types the outputs becomes Nan.This is same for other operators expect the addition operator.
**Increment**
PostIncrement:
let i = 5;
let result = i++;
console.log(result); // Output: 5
console.log(i); // Output: 6
PreIncrement:
let i = 5;
let result = ++i;
console.log(result); // Output: 6
console.log(i); // Output: 6
**Decrement**
PostDecrement:
let i = 5;
let result = i--;
console.log(result); // Output: 5
console.log(i); // Output: 4
PreDecrement:
let i = 5;
let result = --i;
console.log(result); // Output: 4
console.log(i); // Output: 4
Top comments (0)