Operators and Expressions
In JavaScript, operators are used to perform operations on variables and values. Expressions are combinations of variables, values, and operators that are evaluated to produce a result.
Operators in JavaScript are symbols that perform specific operations on one or more operands (values or variables). For example, the addition operator(+) adds two operands together and the assignment operator(=) assigns a value to a variable.
There are several types of operators in JavaScript:
Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations on numeric values. Here are some examples of arithmetic operators:
Addition(+): Adds two operands together.Subtraction(-): Subtracts the second operand from the first.Multiplication(*): Multiplies two operands together.Division(/): Divides the first operand by the second.Modulus(%): Returns the remainder of the division of the first operand by the second.
For example:
let x = 10;
let y = 5;
let sum = x + y; // sum is now 15
let difference = x - y; // difference is now 5
let product = x * y; // product is now 50
let quotient = x / y; // quotient is now 2
let remainder = x % y; // remainder is now 0Comparison Operators
Comparison operators are used to compare two values and return a Boolean value(true or false). Here are some examples of comparison operators:
Equal(==): Returns true if the two operands are equal.Not Equal(!=): Returns true if the two operands are not equal.Strict Equal(===): Returns true if the two operands are equal and of the same type.Strict Not Equal(!==): Returns true if the two operands are not equal or are of different types.Greater Than(>): Returns true if the first operand is greater than the second.Less Than(<): Returns true if the first operand is less than the second.Greater Than or Equal(>=): Returns true if the first operand is greater than or equal to the second.Less Than or Equal(<=): Returns true if the first operand is less than or equal to the second.
For example:
let a = 10;
let b = 5;
console.log(a == b); // false
console.log(a != b); // true
console.log(a === b); // false
console.log(a !== b); // true
console.log(a > b); // true
console.log(a < b); // false
console.log(a >= b); // true
console.log(a <= b); // falseLogical Operators
Logical operators are used to combine multiple conditions and return a Boolean value. Here are some examples of logical operators:
AND(&&): Returns true if both operands are true.OR(||): Returns true if at least one of the operands is true.NOT(!): Returns true if the operand is false.
For example:
let x = 10;
let y = 5;
console.log(x > 5 && y < 10); // true
console.log(x > 5 || y > 10); // true
console.log(!(x > 5)); // falseAssignment Operators
Assignment operators are used to assign values to variables. Here are some examples of assignment operators:
Assignment(=): Assigns a value to a variable.Addition Assignment(+=): Adds a value to a variable and assigns the result to the variable.Subtraction Assignment(-=): Subtracts a value from a variable and assigns the result to the variable.Multiplication Assignment(*=): Multiplies a variable by a value and assigns the result to the variable.Division Assignment(/=): Divides a variable by a value and assigns the result to the variable.
For example:
let x = 10;
x += 5; // x is now 15
x -= 5; // x is now 10
x *= 2; // x is now 20
x /= 2; // x is now 10Ternary Operator
The ternary operator is a shorthand way of writing an if-else statement. It takes three operands: a condition, a value to return if the condition is true, and a value to return if the condition is false. For example:
let x = 10;
let result = x > 5 ? "greater than 5" : "less than or equal to 5";
console.log(result); // "greater than 5"In this example, if x is greater than 5, the value of result will be "greater than 5"; otherwise, it will be "less than or equal to 5".
We will cover more about operators and expressions in the upcoming tutorials.
Expressions
Expressions are combinations of variables, values, and operators that are evaluated to produce a result. For example:
let x = 10;
let y = 5;
let sum = x + y; // sum is now 15
let isGreater = x > y; // isGreater is now trueIn this example, x + y is an expression that adds the values of x and y together, and x > y is an expression that compares the values of x and y to determine if x is greater than y.
By using operators and expressions effectively in your programs, you can perform a wide range of operations and calculations to manipulate data and control the flow of your code. This allows you to create more dynamic and interactive applications that respond to user input and produce meaningful output.
Conclusion
Operators and expressions are fundamental concepts in JavaScript that allow you to perform operations on variables and values. By understanding how to use these concepts effectively, you can write more powerful and expressive code that accomplishes a wide range of tasks. Experiment with different operators and expressions in your programs to see how they can be used to create dynamic and interactive applications.
How is this guide?
Sign in to share your feedback
Help us improve by sharing your thoughts on this guide.
Last updated on
Primitives and Objects
In JavaScript, variables can store two types of values:- primitives and objects. Understanding the difference between these two types is important for writing effective and efficient code.
var vs let vs const
In JavaScript, there are three ways to declare variables:- var, let, and const. Understanding the differences between these three methods is important for writing clean and maintainable code.