Operators & Expressions in JS
This lesson lists the commonly used JavaScript's expressions and operators, including assignment, comparison, arithmetic, bitwise, logical and string.
We'll cover the following...
Operators
JavaScript operators can be categorized into two main categories i.e., Unary and Binary operators. Unary takes only one operand whereas binary takes two.
1. Binary Operators
Binary operators can further be divided into following types:
- Arithmetic Operators ()
- Assignment Operators (=, +=, -=, *=, /=)
- Logical Operators (&&, ||, ! )
- Comma Operator ( , ): The Comma operator evaluates each operand from left to right and returns the value of right most operand.
- Comparison Operators ()
- Bitwise Operators (&, |, ^)
- String Operators ()
- Conditional Operators ()
Did you know? A comma operator (
,) is used when you want to evaluate an expression from left to right.
2. Unary Operators
As mentioned earlier, unary operators take only one operand in order to perform a specific operation. Some of the commonly used unary operators in JavaScript are:
typeof: Returns the type of the given operanddelete: Deletes an object, object’s attribute or an instance in an arrayvoid: Specifies that an expression does not return anything- Increment Operators :
Did you know? The operator
===is commonly referred as strict equality operator in JavaScript. The only difference between double equals==and deep equals is that the former does not perform type comparison but in fact, converts the type of one of the operands to make their types same. strict equality operator, on the other hand, returns false if both types are not the same.
See the example given below for better understanding:
Expressions
Anything that evaluates to a value is called an expression. Some of the basic expressions and keywords used in JavaScript are mentioned below:
this: points to the current objectsuper: calls methods on an object’s parent, for example, call parent’s constructorfunction: used to define a functionfunction*: used to define a generator functionasync function: used to define an async function
There is one special operator which we have left for now… let’s discuss it in the next lesson!