postImage

Tutorial: Javascript Operator

blogImage

By Emmanuel Chinonso

Web Developer

JavaScript Operator

Contrast Bootstrap UI Kit

What is an Operator?

Let's look at a simple example. The expression 3 + 8 will give you 11. The operands in this case are 3 and 8, and the operator is '+'. JavaScript supports all this operators.

Assignment operator: The assignment operator (=) is used to give a variable a value.

Adding operator: You can use the plus sign (+) in adding two numbers together.

The Multiplying operator: The multiplication operator (*) is used to multiply two or more numbers.

A table of some operators can be found below.

JavaScript Arithmetic Operators

To perform arithmetic on numbers, arithmetic operators are used:

OperatorDescription
+Addition
------
-Subtraction
------
*Multiplication
------
**Exponentiation (ES6)
------
/Division
------
%Modulus (Division Remainder)
------
++Increment
------
--Decrement

JavaScript Assignment Operators

JavaScript variables are assigned values using assignment operators.

OperatorExampleSame As
=x = yx = x + y
---------
+=x += yx = x + y
---------
-=x -= yx = x - y
---------
*=x *= yx = x * y
---------
/=x /= yx = x / y
---------
%=x %= yx = x % y

The assignment operator (+=) is used to add a value to a variable.

JavaScript code

js
var x = 10;
x += 5; // Result 15

JavaScript String Operators

You can also use the + operator to join (concatenate) strings.

JavaScript code:

js
var txt1 = 'Johnny';
var txt2 = 'Smith';
var txt3 = txt1 + ' ' + txt2;

The + operator is known as the concatenation operator when applied to strings.

Adding Strings and Numbers

When you put two numbers together, you'll get the sum, but when you combine a number and a string, you'll get a string:

JavaScript code

js
var x = 5 + 5; // Result 10
var y = '5' + 5; // Result 55
var z = 'Hello' + 5; // Result Hello5

Adding a number to a string will simply give you a string.

JavaScript Comparison Operators

OperatorDescription
==equal to
------
===equal value and equal type
------
!=not equal
------
!==not equal value or not equal type
------
>greater than
------
<less than
------
>=greater than or equal to
------
<=less than or equal to
------
?operator job

JavaScript Logical Operators

OperatorDescription
&&logical and
------
IIlogical or
------
!logical not

JavaScript Type Operators

OperatorDescription
typeofReturns the type of a variable
------
instanceofReturns true if an object is an instance of an object type.

Contrast Bootstrap UI Kit

Build modern projects using Bootstrap 5 and Contrast

Trying to create components and pages for a web app or website from scratch while maintaining a modern User interface can be very tedious. This is why we created Contrast, to help drastically reduce the amount of time we spend doing that. so we can focus on building some other aspects of the project.

Contrast Bootstrap PRO consists of a Premium UI Kit Library featuring over 10000+ component variants. Which even comes bundled together with its own admin template comprising of 5 admin dashboards and 23+ additional admin and multipurpose pages for building almost any type of website or web app.
See a demo and learn more about Contrast Bootstrap Pro by clicking here.

ad-banner

Related Posts

Comments

...