Javascript Operators

An operator is a mathematical sign that holds two or more operand to perform a specific operation on those operands depending on the specific operator. Like all other programming languages, Javascript also has many built-in standard operators to perform a specific calculation or make a decision. In this tutorial, we will learn Javascript Operators and all the possible operator types.

Javascript Operators and Types

Javascript has built-in following 8 types of operator for performing different kinds of calculations. See the following list of Javascript operators:

String operator

In the previous tutorial, we have learned how to connect between two or multiple strings together. We had used the mathematical  sign to connect strings together. In case of string connection in Javascript, + is the only sign that is considered as string operator. Otherwise, the + sign is considered as arithmetic addition operator. Let’s see an example to clear it.

String Operator Example:
var myString1 = "Welcome ";
var myString2 = "to ";
var myString3 = "PrograCoding";
console.log( myString1 + myString2 + myString2);

// or you can directly use in a context
console.log( "Welcome " + "to " + "PrograCoding");

 

Arithmetic Operators

Arithmetic operators are the basic operators for performing calculation among numerical values. In general, addition, subtraction, multiplication, division, and modulus are considered as basic arithmetic operators in Javascript.

Arithmetic operators
Operator Description
Addition ( + ) Simply add two or more numerical numbers (eg: 2 + 5 = 7)
Subtraction ( - ) Subtract two numbers from one to another (e.g: 5 - 2 = 3)
Multiplication ( * ) Multiplies two or more numerical values (e.g: 4 * 7 = 28)
Division ( / ) Simple divide two numbers (e.g: 10 / 2 = 5)
Reminder ( % ) It actually consists of division reminder (eg: 21 / 4 = 1)

Some arithmetic operators or combination of arithmetic operators may be referred to as other operators. For instance, now, x++ is an increment operator here, ++ is an increment operator. We will talk about the derivatives operators later that was produced from arithmetic operator’s sign.

For now, Let’s see how to implement basic arithmetic operators in javascript:

Arithmetic operators example:
console.log (4 + 9); // addition
console.log(12 - 4); //subtruction
console.log(10 * 8); //multiplication
console.log(80 / 10) //division
console.log(35 /16) //reminder

 

Output in the JS console

> 13
> 8
> 80
> 8
> 3

We will learn more about arithmetic operators in details in a later chapter.

Assignment Operators

Assignment operators used in Javascript to assign a value or something else to a variable. In javascript, the assignment operator is represented by an arithmetical equal sign ( = ). While declaring a variable the assignment operator is used to assign the value. var x = 5 in Javascript indicates that 5 will be assigned to x. It does not mean x is equal to 5.

Assignment Operators Example:
var x = 10; // 10 has been assigned in the variable x

document.write ( x );
// expected output 10. Because x has hold 10 as its value as defined

 

There are more kinds of assignment operators exist in Javascript for ease of use while writing source codes. We will learn in details about the assignment operator in a later chapter.

Comparison Operators

In Javascript, comparison operators are repeatedly used in Javascript condition. We have no yet Javascript condition. But we will learn in the later chapters. Comparison operators actually compare two or more operands. The operands can be either numerical values or string value or even it can be objects.

The result of the comparison operator always returns boolean values, TRUE or FALSE. In general, In Javascript conditional operation, if a condition met TRUE using a comparison operator. Then a certain block of code will be executed otherwise will execute another block.

In Javascript,

  • == sign compares two operands that if they are true in value.
  • === sign compare two operands that if they are true in value and type.
  • != sign compare two operands that if they are false.
Comparision Operator Example:
var x = 6;
console.log( x == 3);
console.log( x != 3 );
console.log( x = 6);

 

Output:

> false
> true
> true

We will learn in details about all the comparison operators and their method in a later chapter.

Logical Operators

In general, logical operators are used with boolean values but it also can be used with non-boolean values. When it is used with boolean values then it returns boolean values and when it is used with non-boolean values it returns non-boolean values.

Operator Description
Logical AND ( && ) If both two operands are true then returns true, otherwise returns false
Logical OR ( || ) If one of the operands is true then returns true, if both are false then return false.
Logical NOT ( ! ) If a single operand can be converted to true then it returns false otherwise returns true.

Ternary Operators

Ternary operators are one kind of special conditional operator in Javascript operators. In some cases, you can you ternary conditional operators to define small pieces of conditions instead of if—else condition. The ternary operator syntax is:

condition ? value1(true) : value2(false);

 

Ternary Operator Example:
var status = ( age >= 18) ? "adult" ; "minor";

 

We will learn more about the ternary operator in details in a later chapter.

Bitwise Operators

Bitwise operator performs binary logical operation internally and represents the operands in specified format rather than binary format. It can decimal, octal or hexadecimal format. But in general, the default format is decimal.

While operation, all the operands is initially converted to binary (zeroes and ones). Then after the operation with the binary format, the final result is represented as default decimal format.

Bitwise Operator example:
console.log( 5 & 13 ); // here & is the Bitwise logical AND operator

//expected output  5

We will learn everything about Bitwise operator in a later chapter.

Unary Operators

Till now, we have learned many operators. Basically, we have worked with two or three operands with one operator for performing different kinds of calculations. But unlike those operators, unary operators are different from them. Unary operators use operand operator or operator operand syntax. The following types consist of unary operators.

  • x++, ++x: It is an increment operator and it is also a subcategory of the unary operator. This operator repeatedly used in Javascript looping. The general uses of the increment operator are to count from a lower value to higher value.
  • x–, –x: It is a decrement operator. It also used in the loop. But unlike increment operators, it counts from a higher value to lower value.
  • delete: It is actually an object used to delete an element. It can be used to delete either an element from an array or object.
  • typeof: It is a useful object for checking javascript data types. It is a unary operator because its syntax is typeof (operand) or typeof operand.
  • void: this operator evaluates a given expression and then the expression terminates working after the first evaluation.

We learn more about unary operators and their types in details in the later chapters.

We will learn every single javascript operator in the later chapters and it’s functions, methods, and properties.

« Previous Next »