Published on: 06 June, 2013

Posted by andy | 1934 views 1 likes 1 favourites 0 comments

In this chapter, you will learn a list of operators available in Javascript. Operators in Javascript can be described as an operation to manipulate data values. Those manipulation can be numbers, strings, booleans or even object values. See below available operators.

In Javascript conditional operators are used to compare both values, if one of the value is equal or not identically equal. See below code example for more details.

// we declare example variables var myFirstFavouriteNumber = 1; var mySecondFavouriteNumber = 8; var myLuckyNumber = 8; //we check the both values using == operator to see if the values are equals if(myLuckyNumber == mySecondFavouriteNumber){ alert("They are the same"); } //we check the both values using != operator to see if the values are not equals if(myFirstFavouriteNumber != mySecondFavouriteNumber){ alert("They are the not same"); }

Boolean Operators represents true or false logical value. The operator symbols used are: **!** and **&&** and **||**. In other word those symbols represent those words in order: **NOT**, **OR**, **AND**. Note the operators == and != can be considered as Equality Operators as well.

//the symbol ! represent not equal or false var isNumber = false; // using ! means, we want to check if the isNumber data type is not equal to true if(!isNumber){ alert("This is not number"); } //lets see another example on how to use the operators && and || var myFirstFavouriteNumber = 1; var mySecondFavouriteNumber = 8; //The operator || represent OR value if(myFirstFavouriteNumber == 1 || mySecondFavouriteNumber == 8){ alert("One of my favourite number is selected"); } //The operator && represent AND value if(myFirstFavouriteNumber == 1 && mySecondFavouriteNumber == 8){ alert("Both of my favourite numbers are selected"); }

In Javascript those operators are used for increasing or descreasing numberic values. See below example for more details.

// integer number var myNumber = 1000; //To increase the value you can use this method. Note both methods will result the value of 1001; myNumber = myNumber + 1; ++myNumber; //To decrease the value you can use this method. Note both methods will result the value of 999; myNumber = myNumber - 1; --myNumber;

There is another short cut conditional operator you can use, if you do not prefer to use if conditional. You will learn more details about If conditional statements in the upcoming Statements chapter. See below code example for more details.

// declare the variables var myNumber1 = 100; var myNumber2 = 200; // this will tell us that if the variable myNumber1 is bigger than myNumber2, it will return the left value which is myNumber1 otherwise myNumber2 will be returned. In this case myNumber2 will be return. var higherNumber = myNumber1 > myNumber ? myNumber1 : myNumber2;

In Javascript those operators are used for mathematical calculations. See below example for more details.

// adding and minusing in math operations var myNumber1 = 1000 + 200; // return value = 1200 var myNumber2 = 1000 - 200; // return value = 800 // multiplying and dividing in math operations var totalNumber = 100 * 20; // return value 2000 var revenue = 100 / 20; // return value 5 // modulus in math operations var finalNumber = 52 % 25 // equal to 2

Comments

There are no comments available.

Write Comment

0 characters entered. Maximum characters allowed are 1000 characters.