Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • JavaScript - Get Started
  • What is JavaScript
  • Setup JavaScript Dev Environment
  • HTML <script> Tag
  • JavaScript - Syntax
  • JavaScript - Popup Message
  • JavaScript - Variables
  • JavaScript - Operators
  • JavaScript - Data Types
  • JavaScript - String
  • JavaScript - Numbers
  • JavaScript - Boolean
  • JavaScript - Object
  • JavaScript - Date
  • JavaScript - Date Methods
  • JavaScript - Array
  • JavaScript - Array Methods
  • JavaScript - null and undefined
  • JavaScript - Function
  • JavaScript - if condition
  • JavaScript - switch
  • JavaScript - for loop
  • JavaScript - while loop
  • JavaScript - Scope
  • JavaScript - eval
  • JavaScript - Error Handling
  • JavaScript - strict mode
  • JavaScript - Hoisting
  • Define JS Class
  • JS Object In Depth
  • this Keyword
  • new Keyword
  • Prototype
  • Inheritance
  • Closure
  • IIFE
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

Javascript Operators

JavaScript includes operators same as other languages. An operator performs some operation on single or multiple operands (data value) and produces a result. For example, in 1 + 2, the + sign is an operator and 1 is left side operand and 2 is right side operand. The + operator performs the addition of two numeric values and returns a result.

JavaScript includes following categories of operators.

  1. Arithmetic Operators
  2. Comparison Operators
  3. Logical Operators
  4. Assignment Operators
  5. Conditional Operators
  6. Ternary Operator

Arithmetic Operators

Arithmetic operators are used to perform mathematical operations between numeric operands.

OperatorDescription
+Adds two numeric operands.
-Subtract right operand from left operand
*Multiply two numeric operands.
/Divide left operand by right operand.
%Modulus operator. Returns remainder of two operands.
++Increment operator. Increase operand value by one.
--Decrement operator. Decrease value by one.

The following example demonstrates how arithmetic operators perform different tasks on operands.

Example: Arithmetic Operation
let x = 5, y = 10;

let z = x + y; //performs addition and returns 15

z = y - x; //performs subtraction and returns 5

z = x * y; //performs multiplication and returns 50

z = y / x; //performs division and returns 2

z = x % 2; //returns division remainder 1
Try it

The ++ and -- operators are unary operators. It works with either left or right operand only. When used with the left operand, e.g., x++, it will increase the value of x when the program control goes to the next statement. In the same way, when it is used with the right operand, e.g., ++x, it will increase the value of x there only. Therefore, x++ is called post-increment, and ++x is called pre-increment.

Example: Post and Pre Increment/Decrement
let x = 5;

x++; //post-increment, x will be 5 here and 6 in the next line

++x; //pre-increment, x will be 7 here  

x--; //post-decrement, x will be 7 here and 6 in the next line

--x; //pre-decrement, x will be 5 here
Try it

String Concatenation

The + operator performs concatenation operation when one of the operands is of string type. The following example demonstrates string concatenation even if one of the operands is a string.

Example: + Operator with String
let a = 5, b = "Hello ", c = "World!", d = 10;

a + b; //returns "5Hello "

b + c; //returns "Hello World!"

a + d; //returns 15

b + true; //returns "Hello true"

c - b; //returns NaN; - operator can only used with numbers
Try it

Comparison Operators

JavaScript provides comparison operators that compare two operands and return a boolean value true or false.

OperatorsDescription
==Compares the equality of two operands without considering type.
===Compares equality of two operands with type.
!=Compares inequality of two operands.
>Returns a boolean value true if the left-side value is greater than the right-side value; otherwise, returns false.
<Returns a boolean value true if the left-side value is less than the right-side value; otherwise, returns false.
>=Returns a boolean value true if the left-side value is greater than or equal to the right-side value; otherwise, returns false.
<=Returns a boolean value true if the left-side value is less than or equal to the right-side value; otherwise, returns false.

The following example demonstrates the comparison operators.

Example: JavaScript Comparison Operators
let a = 5, b = 10, c = "5";
let x = a;

a == c; // returns true

a === c; // returns false

a == x; // returns true

a != b; // returns true

a &gt; b; // returns false

a < b; // returns true

a >= b; // returns false

a &lt;= b; // returns true
Try it

Logical Operators

In JavaScript, the logical operators are used to combine two or more conditions. JavaScript provides the following logical operators.

OperatorDescription
&&&& is known as AND operator. It checks whether two operands are non-zero or not (0, false, undefined, null or "" are considered as zero). It returns 1 if they are non-zero; otherwise, returns 0.
|||| is known as OR operator. It checks whether any one of the two operands is non-zero or not (0, false, undefined, null or "" is considered as zero). It returns 1 if any one of of them is non-zero; otherwise, returns 0.
!! is known as NOT operator. It reverses the boolean result of the operand (or condition).!false returns true, and !true returns false.
Example: Logical Operators
let a = 5, b = 10;

(a != b) && (a < b); // returns true

(a > b) || (a == b); // returns false

(a &lt; b) || (a == b); // returns true

!(a < b); // returns false

!(a > b); // returns true
Try it

Assignment Operators

JavaScript provides the assignment operators to assign values to variables with less key strokes.

Assignment operatorsDescription
=Assigns right operand value to the left operand.
+=Sums up left and right operand values and assigns the result to the left operand.
-=Subtract right operand value from the left operand value and assigns the result to the left operand.
*=Multiply left and right operand values and assigns the result to the left operand.
/=Divide left operand value by right operand value and assign the result to the left operand.
%=Get the modulus of left operand divide by right operand and assign resulted modulus to the left operand.
Example: Assignment operators
let x = 5, y = 10, z = 15;

x = y; //x would be 10

x += 1; //x would be 6

x -= 1; //x would be 4

x *= 5; //x would be 25

x /= 5; //x would be 1

x %= 2; //x would be 1
Try it

Ternary Operator

JavaScript provides a special operator called ternary operator :? that assigns a value to a variable based on some condition. This is the short form of the if else condition.

Syntax:
<condition> ? <value1> : <value2>;

The ternary operator starts with conditional expression followed by the ? operator. The second part (after ? and before :) will be executed if the condition turns out to be true. Suppose, the condition returns false, then the third part (after :) will be executed.

Example: Ternary operator
let a = 10, b = 5;

let c = a > b? a : b; // value of c would be 10
let d = a &gt; b? b : a; // value of d would be 5
Try it
TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.