JavaScript operators, arithmetic, unary, Strings

5/5 - (1 vote)

Assignment operators

The assignment operators are used to assign values ​​to variables, like:

var x =11;  // assign the value 11 to x

They Assign the value of the right side of the equal sign to the left of the equal sign.

Short form   Long form
= 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
+=, -=, *=, /=, %= are single operators, this means that there are no spaces between symbols.

 

Arithmetic operators

The arithmetic operators are used to perform arithmetic operations with numbers.

+ Addition
Subtraction
* Multiplication
** Exponentiation
/ Division
% Modulus
++ Increment
– – Decrement
As in most programming languages, in JavaScript, operators precedence evaluates the order of operators in an expression.
For example, in algebra, multiplication, and division have higher precedence than addition or subtraction.

++ and — operator

Are called as unary operators,
The plus plus operator ++ (prefix), which increments by 1, and minus minus operator – – (postfix) which decrements by 1, can be used either after the variable name or before the variable name, like a++ or ++a.

a++

var a = 1;
alert (a++); //The result will be: 1
alert (a); //The result will be: 2

++a

var a = 1;
alert (++a); //The result will be: 2
alert (a); //The result will be: 2

The end result is the same in both cases, that the variable a=2, only the alert message was different!

 

String Operators

String concatenation in JavaScript

Prior to ES6, the old way to concatenate strings together was by using the string concatenation operator ( + ).

When is used on strings, the + operator is named the concatenation operator.

Example:
var str1 = "Hello";
var str2 = "world"
alert (str1 + " " +str2); //The result will be: Hello world

Adding a number to a string will return a string:

var a = "Hello";
var b = 5;
alert (a + b); //The result will be: Hello5

Using + operator to concatenate strings it’s ok, but it becomes difficult when we need to build multi-line strings!

As an alternative to using the string concatenation operator ( + ), you can use the String’s concat() method, but both are difficult to use when it comes to build multi-line strings.

const str1 = 'Hello';
const str2 = 'World';

console.log(str1.concat(' ', str2));
// "Hello World"

Comparison and Logical Operators

== equal
=== equal as value and type (strict equality)
!= not equal
!== not equal as value or type (strict unequal)
&& logical and
|| logical or
> greater than
< less than
>= greater or equal
<= less or equal
? ternary
Attention to these three operators:
= Assignement
== Equality
=== Strict equality

!!!One of the most common mistakes is to use the assignment operator instead of the equality operator, like:

var a = 1;
var b = 2;
if (a = b){ 
	alert("They are equal"); //The code in the if statement will always be executed
	} 

This is an assignment and returns true, therefore the code in the if statement will always be executed!
 

Properly is to use the equality operator:

var a = 1;
var b = 1;
if (a == b){ 
	alert("They are equal"); //The result will be: They are equal
	} else {
             alert("They are not equal"); 
               }

 

But if we compare a numerical value to a string using the equality operator, the result will be the same, because they have same value:

var a = 1;
var b = "1";
if (a == b){ 
	alert("They are equal"); //The result will be: They are equal
	} else {
             alert("They are not equal"); 
               }

 

If we want to o check if they have the same value and the same type, we must use the strict equality operator, like:

var a = 1;
var b = "1";
if (a === b){ 
	alert("They are equal"); 
	} else {
             alert("They are not equal"); //The result will be: They are not equal
               }

In other words, this means that they are not just equal, they are identical!

? ternary operator

condition ? true : false
The ternary operator which is not very common, is similar to an if/else statement, the difference is that we can condense the code into a single line:

Example:
var a = 3;
if ( (a%2) == 0){
	alert ("a is an even number");
	} else{
	alert ("a is not an even number"); //The result will be: a is not an even number
	}

is similar with:

var a = 3;
((a%2) == 0) ? alert ("a is an even number") : alert ("a is not an even number"); 
//The result will be: a is not an even number

 

Type Operators

Can be used to find the data type of a JavaScript variable.

typeof returns the type of a variable
instanceof returns true if an object is an instance of an object class

Hello there!

I hope you find this post useful!

I'm Mihai, a programmer and online marketing specialist, very passionate about everything that means online marketing, focused on eCommerce.

If you have a collaboration proposal or need helps with your projects feel free to contact me. I will always be glad to help you!

subscribe youtube

Leave a Comment

WebPedia.net