JS Operators Part One
Operator
Operators are the symbols that instruct the Javascript engine to perform an action
For example, + is an operator to add two variables, β is an operator to subtract two variables, =, >, < are the operators to compare two variables.
Operands
The quantity on which an operation is to be done.
Operands are also known as βArgumentsβ.
Javascript supports the following Operators:
1. Arithmetic Operator
2. Assignment Operator
3. Comparison Operator
4. Logical Operator
5. Bitwise Operator
6. Conditional Operator
Revising the Data Types and Variables topics would really help to understand the operators.
1. Arithmetic Operator
The operators used to perform arithmetical operations.
1.1 Increment Operator
Increment Operator (++) increments the variable value by one
Pre-Increment: Increments by one, then return x
Post-Increment: Returns x, then increment by one
What is happening here .? Before incrementing the value, the expression is evaluated. x++ operation is performed after the printing(output) the x value.
Difference between Pre-Increment and Post-Increment Operator
Pre Increment: Increments the value and execute the expression/ statement
Post Increment: Evaluate the expression/ statement, then increments the value
1.2 Decrement Operator
Increment Operator (--) increments the variable value by one
Pre-decrement: Decrements the value by one, then return x
Post-Increment: Returns x, then decrements the value by one
Difference between Pre-decrement and Post-decrement
Pre Decrement: Decrements the value and execute the expression/ statement
Post Decrement: Evaluate the expression/ statement, then decrements the value
The same applies to the decrements
Use cases of Increment/ Decrement Operator
Increment/ Decrement Operator inside an expression
One example for my satisfaction for the understanding of Pre and Post Increment;)
βUnaryβ, βBinaryβ Operators
Unary Operator: An operator with one operand
Binary Operator: An operator with two operands
String Concatenation
The + Operator concatenates two strings
Number as a String
Whatever we writes inside β β is a String. Example : β21β Javascript takes it as a String, not as a number Data Type . So, here 21 is not a number anymore
Rules :
Strings concatenate each other. Numbers are added as normal arithmetic operation.
If any of the operand is a String, then the other operand is converted to a String too. Doesnβt matter whether the first operand or the second operand is a string.
If there are two or more operands and if the third operand is a string followed by two numbers, then the numbers will be added before it being converted to a String.
The above rules are only for binary +. The other arithmetic operators like β, *, / work only with numbers. It converts operands to a Number Data Type before the operation even if it is a String.
Strings to Numbers
We have seen what is unary and binary operators. See how it works to convert a String to a Number
Both string variables women and men are converted to a Number variable before the binary plus operation (Red Box). Code in the green box
Adding + before the variable converts a String variable to a Number variable, Here β+β in the β+women, and +menβ is an unary plus
Booleans to Numbers
Operator Precedence
Operator precedence is the order of execution of an arithmetic expression with more than two operands
Comma Operator
Comma Operator allows us to evaluate several expressions dividing by comma.
If we evaluate and try it assigns it to a variable, both expressions are evaluated, but the first expression result is thrown away and the second expression is evaluated and returns the result, because
2. Assignment Operator
Assignment operator β=β assign the values to the variables.
Various assignment operators
Other arithmetic operators like +, β, *, /, % are higher in order than assignment operator β=β except yield and comma.
Last updated
Was this helpful?