JS Operators
#
WhyJavaScript includes operators as other languages do. An operator performs some operation on single or multiple operands (values) and produces a result.
For example 1 + 2
, where the (+
) sign is an operator and 1
is the left operand and 2
is the right operand.
The (+
) operator adds two numeric values and produces a result which is 3 in this case.
#
WhatJavaScript operators are divided into three main groups based on the number of operands and operators. There are unary and binary operators, as well as a special ternary (or conditional) operator.
#
Unary OperatorsA unary operator requires a single operand, either before or after the operator:
#
Binary operatorsA binary operator requires two operands, one before the operator and one after the operator:
The primary types of binary operators are:
Assignment Operators
Assignment operators are used for assigning values to variables.
*The most common assignment operator is the (
=
) signArithmetic Operators
Often, the logic we write requires and/or is enhanced by common arithmetic. Arithmetic involves one operator between two operands.
Comparison Operators
A comparison operator compares the operands and returns a value based on whether the comparison is true or false.
Logical operators
A logical operator is used to compare two expressions. This typically returns a boolean value.
#
How#
Arithmetic operatorsAddition
Subtract
Multiplication
Divide
Modulus/ Division Remainder
Exponentiation
Increment (post/pre)
#
ConcatenationConcatenation involves combining two strings operands together with the +
operator.
#
Comparison OperatorsLoose Equality
==
Loose Inequality
!=
Greater Than
>
Less Than
<
Equal to or Greater Than
<=
Equal to or Lesser Than
>=
Strict Equality
===
The strict equality operator checks whether two operands are equal, returning a Boolean result. Unlike the loose equality operator, the strict equality operator always considers operands of different types to be different.
Strict Inequality
!==
#
Logical operatorsLogical AND
&&
Logical AND returns
true
only if both operands equate totrue
. Otherwise, it returnsfalse
.Logical OR
||
Logical OR returns
true
only if one or more operand equates totrue
. Otherwise, it returnsfalse
.Logical NOT
!
Logical NOT returns the opposite Boolean value from what an operand equate to.
#
Type Coercion, and Truthy and Falsy ValuesType Coercion is the process of converting, or coercing, a value type from one data type to another to perform an action. We will see this quite often in JavaScript. In fact, the only operations where type coercion can not happen is with strict equality and strict inequality.
For example, let's say we are attempting to add numbers together. One of the values is as we expect: a number value. However, the other value is a string. This creates a problem. You cannot work arithmetic with strings, because arithmetic must involve numbers. So, JavaScript uses type coercion to resolve the problem for you.
But JavaScript gives us a string of "1011"
as the result? Evidently type coercion changed the one number value to a string to perfom concatenation. In fact, type coercion always chooses concatenation if you have two operands (at least one of them a string value) and a +
operator.
Now let's finish this example by looking at other arithmetic operators.
We changed the addition to subtraction and we actually get a number result. That's because -
is used in arithmetic only, and JavaScript clearly knows that you are attempting arithmetic, so both operands need to be numbers.
We also added a comparison operation between 10
and "11"
. JavaScript correctly changed the data types to perform the comparison.
Now let's take a look at how Javascript uses type coercion when checking equality:
- Loose Equality, which enables type coercion
- Strict Equality, which disables type coercion
For example:
We cannot do this with arrays however, even if 2 arrays have the same exact contents, both arrays point to different places in memory:
As well as a data type, each value also has an inherent Boolean value, generally known as either truthy or falsy.
According to the Mozilla Developer Network,
A falsy value is a value that is considered false when encountered in a Boolean context.
#
Falsy ValuesThe following all have boolean value of false:
false
0
0n
""
,''
, ``null
undefined
NaN
- Not A Number
For Example:
All other values are considered “truthy”
Knowing how JavaScript will evaluate values in certain contexts, it can be extremely useful to include truthy/falsy values in our programs.
#
Takeaways- Operators are symbols that initiate an operation between one or more values
- JS Operators include arithmetic, assignment, logical and comparison
- All data types have an inherent Boolean value called truthy or falsy
- Binary operations must be performed on the same data types. JavaScript will use type coercion to change the data types of values in operations if two operands are different.