JavaScript Operators

In this lession we will see the use of operators. Whenever we assign any value to any variable it will be auto casted to best matching type, so when we use any operator between variables it will never throw error, but will give some result.

Mathematical operators

There are basic 4 operators in JavaScript like other programming language as below

+ (addition)
- (subtraction)
* (multiplication)
/ (division)

In JavaScript + (addition) operator is used for two different operations, first we can use for addition of two number and second is concatenation of strings

If we use both integer type numbers or variables then it will produce the result as a number, it's very easy. For example,

Example
var sum = 20 + 30;
console.log(sum);	// It will print 50 as output

var number1 = 20;
var number2 = 30;
var sum = number1 + number2;
console.log(sum);	// It will also print 50 as output

Concating string using addition operator

We can also use addition operator as a concatenating string with another string, number with string or string with number. For example

Example
var age = 30; 
var firstName = "Tarun"; var lastName = "Dudhatra"; console.log("My name is: " + firstName + " " + lastName); console.log(firstName + "'s age is: " + age + " years"); console.log(age + " is the number"); //Above code snippet will print below output //My name is: Tarun Dudhatra //Tarun's age is: 30 years //30 is the number

In JavaScript default primitive value is string type, it will behaves strange when we are using two different types of operands. So when we are try to concate number to a string or string to a number then before concatenation it will transform number to string. If we are adding 1 integer value to another integer value then it will do the sum of two integer values, but if we add 1 integer value to another string value then it will treat integer value a string. For example

Example
var num1 = 4;
var num2 = 3;
var name = "7"
console.log(num1 + num2);	// It will become 7
console.log(num1 + name);	// It will become 47

We have seen + operator, now we will see other operators like subtract, multiply and divide which will uses -, *, slash sign. For example

Example
console.log(99 - 88);	// It will output 11
console.log(9 * 9);	// It will output 81
console.log(99 / 11);	// It will output 9

Advanced mathematical operators

JavaScript supports the modulus operator and it is annotated by % (percentage) sign which will give the reminder of division operation between numbers. I will leave it to you for your exercise.

Like all other language we can also use short expressions for combined expressions, like

+=
-=
*=
/=
%=

When we use any operator with = (equal to) sign then it will become short expressions. To add some value to the varialbe we are writing like number = number + 3, same expression we can write like number += 3;

JavaScript also supports Math module which has more advanced functions to perform arithmetic operations, We will cover those features in other lession.

 

← Previous

Next →