JavaScript for Beginners - Basic Operators (P: VII)

 Math Operators

Arithmetic operators perform arithmetic functions on numbers (literals or variables)

Operator

Description

Example

+

Addition

50 + 5 = 55

-           

Subtraction

20 -10 = 10

*

Multiplication

40 * 2 = 80

/

Division

20 / 4 = 5

%

Modules

56 % 3 = 2

++

Increment

var x = 10; x++, Now x = 11

--

Decrement

var x = 10; x--, Now x = 9

 Addition

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

You can add as many numbers or variables together as you want or need to

Multiplication

The multiplication operator (*) multiplies one number by the other.

Division

The / operator is used to perform division operations:

The Modules

Modules (%) operator returns the division reminder (what is left over)


Increment and Decrement

Increment ++

The increment operator increments the numeric value of its operand by one. If placed before the operand, it returns the increment value. If placed after the operand, ite returns the original value and then increments the operand

Decrement --

The decrement operator decrements the numeric value of its operand by one. If placed before the operand, it returns the decremented value. If place after the operand, it returns the original value and the decrements the operand.

Operator

Description

Example

Result

var++

Post increment

var a = 0, b = 10

var a = b++

a = 10 and b = 11

++var

Pre-increment

var a = 0, b = 10

var a = ++b

a = 11 and b = 11

var--

Post decrement

var a = 0, b = 10

var a = b--

a = 10 and b = 9

--var

Pre-decrement

var a = 0, b = 10

var a = --b

a = 9 and b = 9

Assignment Operators

Assignment operators assign values to JavaScript variables

Operator

Example

Is equivalent to

=

 x = y

x = y

+=

x += y

x = x + y

-=

x -= y

x = x - y

*=

x *= y

x = x * y

/=

x /= y

x = x / y

%=

x %= y

x = x % y


Comparison Operators

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

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

The table below explains the comparison operators

Operator

Description

Example

==

Equal to

5 == 10 false

===

Identical (equal and same type)

5 === 10 false

!=

Not equal to

5 != 10 true

!==

Not identical

10 !== 10 false

> 

Greater than

10 > 5 true

>=

Greater than or equal to

10 >= 5 true

< 

Less than

10 < 5 false

 

Less than or equal to

10 < == false


Note: When using operators, be sure that the arguments are of the same data type: numbers should be compared with numbers, strings with strings, and so on
 
Logical or Boolean Operators

Logical operators also known as boolean operators, evaluate the expression and return true or false values.

The table below explains the logical operators (AND, OR, NOT)

Logical Operators

&&

Returns true, if both operands are true

| |

Returns true, if one operands are true

!

Returns false, if the operand is false, and false, if the operand is true

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


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

-          The first conditions determine 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 these results, the whole expression is found to be true.

Conditional (Ternary) Operator

Another JavaScript conditional operator assigns a value to a variable, based on some conditions,

Syntax: 

For Example:


Note: Logical operates allows you to connect as many expressions as you wish

String Operators

The most useful operator for string is concatenation, represented by the + sign. Concatenation can be used to build strings by joining together multiple strings, or joining strings with other types;

The above example declares and initialize two string variables and then concatenates them.

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


Comments