The concept of Operators is present in almost all programming languages present in today's world. Operators are used to perform certain operations. You often probably use (+) in your daily life to perform addition, but, did you ever notice that this (+) sign is a character with a predefined work, i.e., to add two numbers? In this tutorial, you will explore the different types of Operators in JavaScript. Show
Types of Operators in JavascriptIn JavaScript, there are eight different types of Operators present. They are:
You'll go through each of these in this tutorial on Operators in JavaScript. What Are Arithmetic Operators in Javascript?As the name suggests, you use arithmetic operators to perform mathematical operations like addition, subtraction, multiplication, division, and many other operations.
**Note: The (+) operator in JavaScript is also used to concatenate two Strings. You'll get to know more about it in upcoming sections. There are several different types of arithmetic operators present in JavaScript. Below is the list of all the arithmetic operators present in JavaScript. Implementation of arithmetic operators in a program: var x = 10; var y = 5; document.write("Addition = "+(x+y)+"<br>"); document.write("Subtraction = "+(x-y)+"<br>"); document.write("Multiplication = "+(x*y)+"<br>"); document.write("Division = "+(x/y)+"<br>"); document.write("Modulus = "+(x%y)+"<br>"); document.write("Increment = "+(x++)+"<br>"); document.write("Decrement = "+(x--)+"<br>"); Arithmetic Operators The two variables x & y are defined with values 10 & 5. The different types of arithmetic operators are used in the above code to perform certain mathematical operations like addition, subtraction, multiplication, division, and so on. The output for the above code will be: Operator Precedence in JavaScriptOperator Precedence in JavaScript refers to the order of operations. It evaluates different operators falling under a single expression according to the precedence of operators.
var x = 10; var y = 5; console.log(x+y*(3*2)); /* (10+5*(3*2)) (10+5*(6)) (10+30) 40 (Expected output) */ The output for the above code will be:
var x = 10; var y = 5; document.write("The output will be "+(x/y*2)); /* (x+/y*2) (10/5*2) (2*2) 4 (expected output) */ It evaluates division first in the above example, followed by multiplication because both the operators have the same precedence. Hence the expression is evaluated from left to right. The output for the above example will be: What Are Assignment Operators in Javascript?You use assignment operators to assign values to different JavaScript variables. If you want to assign a value 15 to a variable X, you can do it with the help of the assignment operator. Consider the following example: var x = 10; document.write(x); In the above example, the value assigned to variable x is 10. If you try to print the value variable x is holding on to the browser, then, the output will be 10.
The different types of assignment operators present in JavaScript are: Implementation of assignment operators: var x = 10; var y = 5; console.log(x); // Returns the value of x. console.log(x+=y); // Returns x = x+y console.log(x-=y); // Returns x = x-y console.log(x*=y); // Returns x = x*y console.log(x/=y); // Returns x = x/y console.log(x%=y); // Returns x = x%y console.log(x^=y); // Returns x = x^y console.log(x|=y); // Returns x = x|y console.log(x**=y); // Returns x = x**y The output for the above code will be: What Are String Operators in Javascript?String operators are used for performing operations on strings in JavaScript. You can use the (+) operator to concatenate two different strings, or the (+=) can also be used to add something to an existing string. Implementation of string operators. var x = "Hello"; // Variable x with string value var y = "World"; // Variable y with string value document.write(x+y+"<br>") // (+) operator will concatenate both the strings document.write(x += "Everyone"); // (+=) operator will add "Everyone" to the existing String
The output of the above code will be: What Are Comparison Operators in Javascript?Comparison operators are also used to check for true and false. As the name suggests, you use a comparison operator to compare two values or variables using different operators. Several comparison operators are present in JavaScript. Below is the list of comparison operators:
Implementation of comparison operators: var x = 5; // Declaring variable x //comparing c with different values console.log(x==8); // Return False (x<8) console.log(x===5); // Return True console.log(x!=8); // Return True (x!=8) console.log(x!==5); // Returns False console.log(x>8); // Returns false (x<8) console.log(x<8); // Returns true because x = 5 console.log(x>=5); // Returns True (x = 5) console.log(x<=8); // Returns True (x < 8)
The output for the above code will be:
What Are Logical Operators in Javascript?You use logical operators to determine the logic between two variables or values. Logical operators return boolean values and check whether multiple conditions are true or false.
var x = 5; // Declaring variable x var y = 8; document.write(x > 4 && y > 7) // Return True Because both the conditions are True document.write("<br>");// Using <BR> for space. document.write(x < 7 && y > 8); // Return False because y is not greater than 8 The output for the above code will be:
var x = 5; // Declaring variable x var y = 8; document.write(x > 6 || y > 9); // Return False because both the conditions are False document.write("<br>");//Using<BR>for space. document.write(x < 7 || y > 8); // Return true because one condition holds true The output for the above code will be:
var x = 5; // Declaring variable x var y = 8; document.write(!(x==y)); // Return True because x is not equal to y. The output for the above code will be: What Are Conditional Operators in Javascript?The conditional operator in JavaScript is used to assign a value to the variable based on a condition. It contains a condition, and for every condition, there are only two possibilities, either true or false.
Refer to the following code for the above example: var age = 20; //Age of person is 20 var x = (age >=18) ? "Old enough" : "Too Young"; // variable x will hold the value document.write(x); // Return "Old enough" The output will be: What Are Bitwise Operators in Javascript?This operator doesn't have much practical usage in the real world, although they are not useless. But, this is something you are not going to use daily. In computers, the numbers are stored in binary format. Bitwise operators work on 32-bit numbers.
Implementation of bitwise operators: var x = 5; var y = 1; document.write((x & y)+"<br>"); // works the same as logic gates document.write((x | y)+"<br>"); // works the same as logic gates document.write((~x)+"<br>"); // Returns (-6) and works the same as (-x-1) document.write((x ^ y)+"<br>"); // Returns 4 and works the same as XOR gate. For the XOR gate, the value will be true only when 1 of the two values is true. If both values are true, then the final value will be false. The output will be: Type-of Operator in JavaScriptThe type of() operator in JavaScript will tell you the kind of value a variable holds, whether it is a number, string, or boolean value. It can also differentiate between null and undefined. These 5 are the primitive data types present in JavaScript, and the type of() operator can tell you about which data type a variable is holding. Consider the following code: var x = 5; // Numeric value var y = "hello"; // String value var flag = false; // Boolean Value var p; // No value document.write(typeof(x)+"<br>"); // Return number document.write(typeof(y)+"<br>"); // Return String document.write(typeof(flag)+"<br>"); // Return Boolean document.write(typeof(p)+"<br>"); // Return undefined The output of the above code will be:
var x = null; // variable with value as NULL document.write(typeof(x)); // Returns Object var y; // Variable with no value document.write(typeof(y)); // Returns undefined The output of the above code will be: With this, you have reached the end of this "Operators in JavaScript" tutorial. Master the complete JavaScript fundamentals, jQuery, Ajax, and more with the Javascript Certification Training Course. Check out the course preview! ConclusionIn this tutorial, you explored the concept of Operators. There are 8 different types of Operators present, with each one consisting of a few more within. You also went through the precedence of Arithmetic Operators. Following this, you understood how to use different types of Operators while programming. You also encountered the coding implementation of Type-of Operator. Are you interested in expanding your knowledge of JavaScript or looking for online JavaScript programming training? If the answer is yes to either or both the above questions, the JavaScript certification course offered by Simplilearn is something you should explore. This applied training program is designed to help you grasp the concepts of JavaScript from basics to advance. On that note, if you have any doubts about this tutorial on Operators in JavaScript, don't hesitate to place them as comments at the end of this page; we will respond to them soon! What is a operator in JavaScript?An operator is capable of manipulating a certain value or operand. Operators are used to perform specific mathematical and logical computations on operands. In other words, we can say that an operator operates the operands. In JavaScript operators are used for compare values, perform arithmetic operations etc.
What is operator with an example?In mathematics and computer programming, an operator is a character that represents a specific mathematical or logical action or process. For instance, "x" is an arithmetic operator that indicates multiplication, while "&&" is a logical operator representing the logical AND function in programming.
How many types of operators in JavaScript?JavaScript Operators are as rich as what you'd expect from any modern language. There are four categories: arithmetic, comparison, assignment, and logical.
What are the 4 types of operators?Types of Operators in C and C++. Arithmetic Operators. It includes basic arithmetic operations like addition, subtraction, multiplication, division, modulus operations, increment, and decrement. ... . Relational Operators. ... . Logical Operators. ... . Assignment Operators. ... . Bitwise Operators.. |