24 Feb JavaScript Operators
Operators perform operations by taking one or more values, to give another value. These operations are performed on variables and values. Operators in JavaScript are discussed here. For example:
1 2 3 |
a + b |
The following are the operators in JavaScript:
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Conditional Operators
Let us understand the operators one by one:
JavaScript Arithmetic Operators
Arithmetic operators in JavaScript perform arithmetical operations, such as addition, subtraction, division, multiplication, etc.
Let us see an example to understand the Arithmetic Operators in JavaScript:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<!DOCTYPE html> <html> <body> <h1>JavaScript Arithmetic Operator</h1> <p id="test"></p> <script> let a = 10; let b = 20 let res = a + b document.getElementById("test").innerHTML = "Sum = "+res; </script> </body> </html> |
Output
JavaScript Assignment Operators
Use the Assignment Operator when you need to assign values to a variable. It also includes the Arithmetic Assignment Operators:
Let us see an example to understand the Assignment Operator in JavaScript:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<!DOCTYPE html> <html> <body> <h1>JavaScript Assignment Operator</h1> <p id="test"></p> <script> let a = 25; a += 50; document.getElementById("test").innerHTML = "Sum = "+a; </script> </body> </html> |
Output
JavaScript Relational/ Comparison Operators
Compare two values with JavaScript relational operators, which are also known as Comparison Operators. Let us say a = 3, b =5
Let us see an example to understand the Relational Operator in JavaScript:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<!DOCTYPE html> <html> <body> <h1>JavaScript Relational Operator</h1> <p id="test"></p> <script> let a = 25; let b = (a == 25) document.getElementById("test").innerHTML = "Are they equal? "+b; </script> </body> </html> |
Output
JavaScript Logical Operators
Logical operators combine conditional statements. Considering Boolean variables, a and b. Let us see an example of Logical Operators in JavaScript:
Let us see an example to understand Logical Operators in JavaScript:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<!DOCTYPE html> <html> <body> <h1>JavaScript Logical Operator</h1> <p id="test"></p> <script> let a = true; let b = false; document.getElementById("test").innerHTML = (a&&b) </script> </body> </html> |
Output
JavaScript Conditional Operator
Conditional operator evaluates Boolean expressions, with three operands. It is also known as the ternary operator. The role is to assign a value to a variable, from two given set options. The following is the syntax:
1 2 3 |
variable_name = (expression) ? value1 ( if true) : value2 (if false) |
As shown above, if the condition is true, value 1 will get displayed, else value 2.
Let us see an example to implement the Conditional Operator in JavaScript:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<!DOCTYPE html> <html> <body> <h1>JavaScript Conditional Operator</h1> <p id="test"></p> <script> let marks = 90; res = (marks > 50) ? "Student Passed" : "Student Failed"; document.getElementById("test").innerHTML = res; </script> </body> </html> |
Output
No Comments