JavaScript Basis — Operators

Okonu Deborah
6 min readJun 1, 2020

--

JavaScript is one of the most popular programming languages on earth and it is used to add interactivity to web-pages, process data, as well as creates various applications (mobile apps, desktop apps, games, and more). JavaScript are used primarily by Web browsers to create a dynamic and interactive experience for the user. Most of the functions and applications that make the Internet indispensable to modern life are coded in some form of JavaScript.

Learning the fundamentals of a language will enable you to create the language you desire, whether client-side or server-side.

JavaScript Operators

JavaScript Operators

Types Of JavaScript Operators

Arithmetic Operators

Arithmetic operators are used to perform arithmetic between variables and/or values. They perform arithmetic functions on numbers (literals or variables).

In the example below, the addition operator is used to determine the sum of two numbers.

<!DOCTYPE html>

<html>

<head>

<title>Arithmetic Operators</title>

</head>

<body>

<div id=”demo”></div>

<script>

var x, y, z;

x = 5

y = 4

z = x + y;

document.getElementById(‘demo’).innerHTML = ‘The Result is’ + “ “ + z;

</script>

</body>

</html>

Assignment Operators

Assignment Operators are used to assign values to JavaScript variables.

Example

Assign Values to variables and ass them together:

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

var y = 2; //assign the value 2 to y

var z = x + y //assign the value 7 to z

String Operators

The most useful operator for strings is Concatenation, represented by the + sign.

The + operator and the += operator can also be used to concatenate(add) strings.

When used on strings, the + operator is called The Concatenation Operator.

Concatenation can be used to build strings by joining together multiple strings, or by joining strings with other types.

Example:

var myString1 = “I am learning”;

var myString2 = “JavaScript”;

document.write(myString1 + myString2);

the above example declares and initializes two string variables and then concatenates them.

Numbers in quote are treated as strings: “42” is not the number 42, it is a string that includes two characters, 4 and 2.

Example:

txt1 = “What a very”;

txt2 =”nice day”;

txt3 =”txt1 + “ “ txt2;

The result of txt3 will be:

What a very nice day.

Adding Strings and numbers. Adding two numbers will return the sum, but adding a number and a string will return a string.

Example:

x = 5 + 5;

y = “5” + 5;

z + “Hello” + 5;

The result of x,y, and z will be:

10

55

Hello5

If you add a number and a string, the result will be a string!.

Comparison Operators

Comparison operators are used in logical statements to determine equality or difference between variables or values. They return or false.

The equal to (==) operator checks whether the operands’ values are equal.

Comparison operators always return true or false.

Note: When using operators, be sure that type arguments are the same data type; numbers should be compared with numbers, strings with strings, and so on.

Logical Operators

Logical operators are used to determine the logic between variables or values

Logical operators also known as “Boolean Operators”, evaluate the expression and return true or false.

In the following example, we have connected two boolean expressions with the AND operator.

document.write((4 > 2) && (10 < 15));

For this expression to be true, both conditions must be true.

— The first condition determines whether 4 is greater than 2, which is true.

— The second condition determines whether 10 is less than 15, which is also true.

Based on this result the whole expression is found to be true.

Conditional (Tenary) Operator

The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark ( ? ), then an expression to execute if the condition is true followed by a colon ( : ), and finally the expression to execute if the condition is false.

JavaScript conditional operator assigns a value to a variable based on some condition.

For example:

var isAdult = (age>18)? “Too young”. “Old enough”;

If the variable age is a value below 18, the value of the variable isAdult will be “Too young”. Otherwise the value of isAdult will be “Old enough”.

Logical operators allow you to connect as many expressions as you wish.

Bitwise Operators

Bit operators work on 32 bit numbers. Any numeric operand in the operation is converted into 32 bit number. The result is converted back to a JavaScript number.

Typeof Operators

The typeof operator is used to get the data type (returns a string) of its operand. The operand can be either a literal or a data structure such as a variable, a function, or an object. … There are six possible values that typeof returns: object, boolean, function, number, string, and undefined.

The typeof operators returns the type of variable, object, function or expression:

Example:

typeof “John” //Returns string

typeof 3.14 //Returns number

typeof NaN //Returns number

typeof false //Returns boolean

typeof [1,2,3,4] //Returns object

typeof {name:’John’, age:35} //Returns object

Note: You cannot use typeof to define is a JavaScript object is an array (or a date).

1

These are the basis of JavaScript operators. Hope you were able to understand how they work.

Remember: It wont work unless you try it.

Please applaud and follow me.

Happy coding❤!!

--

--

Okonu Deborah
Okonu Deborah

Written by Okonu Deborah

A Technica Product/Project Manager || A Virtual Assistant || A lover of Mathematics || A computer science student

No responses yet