Tutorial: Javascript Operator
By Emmanuel Chinonso
Web Developer
JavaScript Operator
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:
Operator | Description |
---|---|
+ | Addition |
--- | --- |
- | Subtraction |
--- | --- |
* | Multiplication |
--- | --- |
** | Exponentiation (ES6) |
--- | --- |
/ | Division |
--- | --- |
% | Modulus (Division Remainder) |
--- | --- |
++ | Increment |
--- | --- |
-- | Decrement |
JavaScript Assignment Operators
JavaScript variables are assigned values using assignment operators.
Operator | Example | Same As |
---|---|---|
= | x = y | x = x + y |
--- | --- | --- |
+= | x += y | x = x + y |
--- | --- | --- |
-= | x -= y | x = x - y |
--- | --- | --- |
*= | x *= y | x = x * y |
--- | --- | --- |
/= | x /= y | x = x / y |
--- | --- | --- |
%= | x %= y | x = x % y |
The assignment operator (+=) is used to add a value to a variable.
JavaScript code
var x = 10;x += 5; // Result 15
JavaScript String Operators
You can also use the + operator to join (concatenate) strings.
JavaScript code:
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
var x = 5 + 5; // Result 10var y = '5' + 5; // Result 55var z = 'Hello' + 5; // Result Hello5
Adding a number to a string will simply give you a string.
JavaScript Comparison Operators
Operator | Description |
---|---|
== | 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
Operator | Description |
---|---|
&& | logical and |
--- | --- |
II | logical or |
--- | --- |
! | logical not |
JavaScript Type Operators
Operator | Description |
---|---|
typeof | Returns the type of a variable |
--- | --- |
instanceof | Returns true if an object is an instance of an object type. |
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.
Related Posts