Operators - JAVASCRIPT: A Beginner’s Guide to Learning the Basics of JavaScript Programming (2015)

JAVASCRIPT: A Beginner’s Guide to Learning the Basics of JavaScript Programming (2015)

Chapter 4. Operators

When you hear of the word operators, it is highly possible that the first things you will think of are the mathematical operators addition (+), subtraction (-), multiplication (*), and division (/). Programming and JavaScript scripting also use these operators.

The biggest difference when it comes to programming operators and mathematical operators is that the former has a lot of different types. Most of the operators in Mathematics are in programming. Technically speaking, math operators are only a subset of programming operators.

So, what are the functions of operators in programming? Primarily, their main function is to manipulate data. In programs or scripts, processing data using these operators are always present. Operators are always in programs whether they were used explicitly or implicitly.

Common Types of Operators

Programming languages have a diverse set of operators.

Arithmetic

The usual mathematical operators are categorized here. And they are:

Addition

+

> var x = 1 + 1

< undefined

> x

< 2

> _

Subtraction

-

> var x = 1 – 1

< undefined

> x

< 0

> _

Multiplication

*

> var x = 2 * 3

< undefined

> x

< 6

> _

Division

/

> var x = 8 / 2

< undefined

> x

< 4

> _

Modulus

%

> var x = 12 % 5

< undefined

> x

< 2

> _

Increment

++

> var x = 1

< undefined

> x

< 1

> x++

< 2

> _

Decrement

--

> var x = 1

< undefined

> x

< 1

> x--

< 0

> _

You can do other mathematical operations in JavaScript. However, you will need to use the Math object and its methods, where the other math operators are included and are available (sin, round, random, pow, etcetera). You will learn more about the Math object later.

Assignment

Assignment operators are there for you to assign values to variables and other elements in your program. The most used assignment operator is the “=” operator. With it alone, you can create a program.

However, there are other assignment operators that you can use to make your script efficient and your life easier.

Add and Assign

+=

> var x = 1

< undefined

> x

< 1

> x += 1

< 2

> _

Subtract and Assign

-=

> var x = 1

< undefined

> x

< 1

> x -= 1

< 0

> _

Multiply and Assign

*=

> var x = 2

< undefined

> x

< 2

> x *= 3

< 6

> _

Divide and Assign

/=

> var x = 8

< undefined

> x

< 8

> x /= 4

< 2

> _

Modulus and Assign

%=

> var x = 12

< undefined

> x

< 12

> x %= 1

< 2

> _

Technically, they are combinations of arithmetic and the assignment operator. When you use them, what happen is that the variable on the left side is treated to be present on the right side of the operator. For example:

> var x = 2

< undefined

> x

< 2

> x += 3

< 5

> _

In the example, x+= 3 is being treated like x = x + 3. At the beginning, you will not be using too much of these operators. However, in big projects, they can become handy. These operators can shorten the time you need to type, and improves the readability of your code.

On a different note, there are other combinations of these operators. Technically, almost all other operators can be shorthanded this way.

String Concatenation Operators

You can also use some operators in strings. There are two primary string operators in JavaScript. Concatenate and concatenate and assign. Here are some examples on how to use them:

> var stringX = "This is a string";

< undefined

> var stringY = ", and you can add or concatenate them using the + operator."

< undefined

> var stringZ = stringX + stringY

< undefined

> stringZ

< "This is a string, and you can add or concatenate them using the + operator."

> _

You can also do the example using the concatenate and assign. For example:

> var stringX = "This is a string";

< undefined

> var stringY = ", and you can add or concatenate them using the + operator."

< undefined

> stringX += stringY

< "This is a string, and you can add or concatenate them using the + operator."

> _

Of course, the usage of the “+” symbol might result to a confusion and curiosity. Can you add numbers and strings? Well, in a way, you can. However, when you do, the result will be a string, and the number will be only concatenated. For example:

> var stringExample = "This is a string."

< undefined

> var x = 2

< undefined

> var y

<undefined

> y = stringExample + x

< "This is a string.2"

> _

Logical and Comparison Operators

In later lessons, you will deal with conditional statements. And when conditional statements are involved, you will need to use logical and comparison operators. If you had a logic class, then you will be familiar with them.

Logical and comparison operators deal only with two possible results: true and false. In different terms, using these two types of operators will let you perform Boolean operations. For you to easily understand what this is all about, in case you are confused, then proceed on checking the table below.

Greater Than

>

> 1 > 2

< false

> 2 > 1

< true

> _

Less Than

<

> 1 < 2

< true

> 2 < 1

< false

> _

Equal To

==

> 1 == 2

< false

> 1 == 1

< true

> _

Not Equal To

!=

> 1 != 2

< true

> 1 != 1

< false

> _

Greater Than or Equal To

>=

> 1 >= 2

< false

> 2 >= 1

< true

> 2 >= 2

< true

> _

Less Than or Equal To

<=

> 1 <= 2

< true

> 2 <= 1

< false

> 2 <= 2

< true

> _

Equal Value and Type

===

> 1 === 1

< true

> 1 === "1"

< false

> _

Not Equal Value and Type

!==

> 1 !== 1

< false

> 1 !== "1"

< true

> _