Javascript Operators simply perform operation and produce a result, there are various of operators, in this lesson we will learn about them and provide examples.
var a = 40; var b = 10; var c = a + b;
var a = 10; var b = 5; var c = a * b;
var a = 5; var b = 10; var c = a + b; // assign 5 and 10 to c (a + b)
Assignment operators are used to assign values to variables.
Operator | Example | Also equal to |
---|---|---|
+= | x += y | x = x + y |
= | x = y | 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 |
var a = 10; a += 5; //comment: we assign and add 5 to a(10), result: 15.
Visit our Javascript Assignment reference to learn the full guide of Assignment operators
We use arithmetic operators to perform arithmetic on numbers:
Operator | Description |
---|---|
* | Multiplication |
/ | Division |
+ | Addition |
- | Subtraction |
** | Exponentiation (ES2016) |
% | Modulus (Division Remainder) |
++ | Increment |
-- | Decrement |
var a = 5; var b = 2; var c = a % b; // the modulus operator (%) returns the division remainder.
Visit our Javascript Arithmetic reference to learn the full guide of Arithmetic operators
Comparison | Description |
---|---|
== | equal to |
> | greater than |
< | less than |
=== | equal value and equal type |
!= | not equal |
!== | not equal value or not equal type |
? | ternary operator |
>= | greater than or equal to |
<= | less than or equal to |
Logical Operators | Description |
&& | logical and |
|| | logical or |
! | logical not |
jake = 10 if (jake > 20) { jake = "Old enough";} else { jake = "Too young"; }
Visit our Javascript Comparisons reference to learn the full guide of Comparison operators
Operator | Description |
---|---|
typeof | Returns the type of a variable |
instanceof | Returns true if an object is an instance of an object type |
Visit our Javascript Type Conversion reference to learn the full guide of Type conversion
You can use the operators you have learned above with strings.
var person1 = "Jake"; var person2 = "Sparrow"; var person3 = person1 + " " + person3; //comment: The result of person3 will be: Jake Sparrow
with FreeCodeWiki, you can learn Javascript and other Web development lessons for free, our main goal is to allow students to learn coding classes in one place for free.