DEV Community

Madhan Raj
Madhan Raj

Posted on

Operators In Javascript.

Types in JavaScript Operators

*JS Arithmetic Operator
*JS Assignement Operator
*JS Relational Operator
*JS Conditional Operator
*JS Comparitional Operator
*JS Logical Operator

Operators are for Mathematical and Logical Computations

The Assignment Operator = assigns values

The Addition Operator + adds values

The Multiplication Operator * multiplies values

The Comparison Operator > compares values

Arithmetic operators perform arithmetic on numbers (literals or variables).

JavaScript Addition
JavaScript Addition

The Addition Operator (+) adds numbers:
Adding

let x = 5;
let y = 2;
let z = x + y;
Enter fullscreen mode Exit fullscreen mode

Subtracting

The subtraction operator (-) subtracts numbers.
Example

let x = 5;
let y = 2;
let z = x - y;
Enter fullscreen mode Exit fullscreen mode

JavaScript Multiplication

The Multiplication Operator () multiplies numbers:
**Multiplying
*

let x = 5;
let y = 2;
let z = x * y;

Enter fullscreen mode Exit fullscreen mode

Dividing

The division operator (/) divides numbers.
Example

let x = 5;
let y = 2;
let z = x / y;

Enter fullscreen mode Exit fullscreen mode

JavaScript Assignment Operators
Assignment operators assign values to JavaScript variables.

Given that x = 10 and y = 5, the table below explains the assignment operators:

Logical Assignment Operators

The = Operator

The Simple Assignment Operator assigns a simple value to a variable.
Simple Assignment Examples

let x = 10;
let x = 10 + y;

Enter fullscreen mode Exit fullscreen mode

The += Operator

The Addition Assignment Operator adds a value to a variable.
Addition Assignment Examples

let x = 10;
x += 5;
Enter fullscreen mode Exit fullscreen mode

The -= Operator

The Subtraction Assignment Operator subtracts a value from a variable.
Subtraction Assignment Example

let x = 10;
x -= 5;
Enter fullscreen mode Exit fullscreen mode

The *= Operator

The Multiplication Assignment Operator multiplies a variable.
Multiplication Assignment Example

let x = 10;
x *= 5; 
Enter fullscreen mode Exit fullscreen mode

The **= Operator

The Exponentiation Assignment Operator raises a variable to the power of the operand.
Exponentiation Assignment Example

let x = 10;
x **= 5; 
Enter fullscreen mode Exit fullscreen mode

The %= Operator

The Remainder Assignment Operator assigns a remainder to a variable.
Remainder Assignment Example

let x = 10;
x %= 5;

Enter fullscreen mode Exit fullscreen mode

JavaScript Comparison

Comparison Operators

Comparison operators are used to compare two values.

Comparison operators always return true or false.

Given that x = 5, the table below explains the comparison operators:

JavaScript String Comparison

All the comparison operators above can also be used on strings:
Example

let text1 = "A";
let text2 = "B";
let result = text1 < text2;

Enter fullscreen mode Exit fullscreen mode

Comparing Different Types

Comparing data of different types may give unexpected results.

When comparing a string with a number, JavaScript will convert the string to a number when doing the comparison. An empty string converts to 0. A non-numeric string converts to NaN which is always false.


Example

age = Number(age);
if (isNaN(age)) {
  voteable = "Input is not a number";
} else {
  voteable = (age < 18) ? "Too young" : "Old enough";
}
Enter fullscreen mode Exit fullscreen mode

JavaScript Conditionals

Conditional Statements

Conditional Statements allow us to perform different actions for different conditions.

Conditional statements run different code depending on true or false conditions.

Conditional statements include:

if
if...else
if...else if...else
switch
ternary (? :)
Enter fullscreen mode Exit fullscreen mode

When to use Conditionals

*Use if to specify a code block to be executed, if a specified condition is true
*Use else to specify a code block to be executed, if the same condition is false
*Use else if to specify a new condition to test, if the first condition is false
*Use switch to specify many alternative code blocks to be executed
*Use (? :) (ternary) as a shorthand for if...else
Enter fullscreen mode Exit fullscreen mode

The if Statement

Use if to specify a code block to be executed, if a specified condition is true.

Syntax
if (condition) {
  // code to execute if the condition is true
}

Enter fullscreen mode Exit fullscreen mode

Top comments (0)