Operators in the Javascript
In this chapter, you will about the operators in the Javascript. Operators perform some kind of operation on the left and right side operands. The operation may addition, subtraction or division. Let’s see the syntax that how operators can be used
[left side operand] [operator] [right side operand] 2 + 2Let’s see the kind of operators in Javascript
Arithmetic operator: The arithmetic operator can perform the addition, subtraction, multiplication and division. Let’s look at the below code snippet
Addition:
// addition let left = 2; let right = 2; let result = left + right; console.log(result); left = 4; right = 4; result = left + right; console.log(result); // output // 4 // 8
Subtraction:
// subtraction let left = 2; let right = 1; let result = left - right; console.log(result); left = 4; right = 2; result = left - right; console.log(result); // output // 1 // 2
Multiplication:
// multiplication let left = 2; let right = 1; let result = left * right; console.log(result); left = 4; right = 2; result = left * right; console.log(result); // output // 2 // 8
Division:
// division let left = 2; let right = 1; let result = left / right; console.log(result); left = 4; right = 2; result = left / right; console.log(result); // output // 2 // 2
Logical operators:
Logical operators are AND (&&) and OR (||). These operators can be used to evaluate one or more conditions to perform some actions. Let’s see the below code snippet.
Let’s see an example for AND (&&) operator:
// logical let user = { name: 'John', isAdmin: true, isActive : true }; if(user.isAdmin === true && user.isActive === true) { console.log('User has admin access'); } else { console.log('User dont have admin access'); } // output // User has admin access
Let’s see an example for OR (||), operator:
// logical let user = { name: 'John', isAdmin: true, isActive : false }; if(user.isAdmin === true || user.isActive === true) { console.log('User has admin access'); } else { console.log('User dont have admin access'); } // output // User has admin access
Typeof operator in Javascript:
Typeof is a kind of operator in Javascript that return type of the variable. Let’s look at some examples
let number = 1; let string = 'Name'; let isAdmin = true; let user = { name: 'John' }; console.log(typeof number); console.log(typeof string); console.log(typeof isAdmin); console.log(typeof user); // out put // number // string // boolean // object
Ternary operator:
The ternary operator is shorthand of if-else condition. The logic is the same as the if-else condition, but it can be done in a single line. Let’s look at some examples.
It is also called as a conditional operator.
let user = { name: 'John', isAdmin: true, isActive : false }; let doesUserHasAccess = user.isAdmin ? 'User has admin access' : 'User doesnt have admin access'; console.log(doesUserHasAccess); // output // User has admin access let user = { name: 'John', isAdmin: false, isActive : false }; let doesUserHasAccess = user.isAdmin ? 'User has admin access' : 'User doesnt have admin access'; console.log(doesUserHasAccess); // output // User doesnt have admin access
One thought on “Operators in the Javascript”
Comments are closed.