javaScript data types and arithmetic operator.

Posted in by Nazrul Islam . Filed in JavaScript


Variables contains several types of data like number, Boolean or String. These are primary data types, let’s start exploring each of them. First one is numbers and it has two format, one is integer without any fraction like var tom =20;. Another one is floating point with decimal value as an example var tom =20.85; 
All javascript numbers are 64 bit floating point, a massive range actually. In practical field we wouldn’t have any number that exceeds JavaScript number range. Numbers could  be positive or negative  form and represented in decimal, octal, or hexadecimal value.  Another primary data types is Boolean type variable, which always hold true or false value. Boolean mostly used to check conditional state. Third one is string. Basically text data are string. An example shown below.

All data inside single or double quote considered as string.  Even we put a number in between quote like var tom = “20”;  is also considered as string data. Now lets talk about using quote. Single or double quote which one should be used? Answer is what ever you like. But we can’t start with a single quote and end up with double quote. If the string itself has any quote then we can make a combination of single and double quote to get rid of this problem. Alternatively you could use a method named escaping,  placing a backslash ( \ ) in front of the quote you want to omit .  Look at this example.

Along with primary data type,  JavaScript also  has two composite and two special data type. Array and object are composite data, we will discuss them as a standalone topic in future. Undefined and null considered as special data type in JavaScript. A variable without assigning value or a object property that doesn’t exists returned undefined output.

Null means a variable contains no valid number, string, array or object. It’s a reserved keyword and not allowed to use it as variable and function name. We can erase a variable data assigning it to null.  Got tired reading these hard terms? Don’t worry it will become more easier as soon as we start practical experiment.

Previously we  use alert function to get variable output output or display any message. It’s a built-in JavaScript function that popup a window, but alert is not necessary for displaying simple test result. We can use a function named console.log(); for this types result, it will display output result in your debugging console.

If you using  Firefox and have Firebug installed click mouse right button then  inspect with firebug. JavaScript Console will appear up in the bottom of the page. I’m using Google Chrome, it has a built in console. For Chrome, go to View>Developer>JavaScript Console or hit CTRL+ALT+J/CMD+OPTION+J. Javascriopt-ConsoleDisplaying output “Spiderman is a super hero” in our console window. Nothing special in code, we just use console.log instead alert function to output a string variable. Markup and script both shown below.

If we want to join these two string into one variable, we have to use a plus (+) sign. Look at this code.

Place this code inside your script tag, output result should be similar to the picture shown below.

Javascript-joining-stringWe join two variable spiderman and ironman  in superhero and log the output result in console. There is a comma following a space  in between them to make the sentence more readable.  We got total three string here, and all joined by a + sign. Bingo! its the concatenation operator that join string. Wait a minute, it just not only concatenation operator but an addition operator too, that returns sum of integer or float type variables.

This code return result like this.

Javascript-joining-number These all simple, but what happens, if we join a string and variable together? It defends on where the variable placed. Look at this example. 

It will perform  sum of 5+9 = 14 then get merged with string ” Cricket Games “. Total output is converted to string and show result like this picture.

Javascript-mixing-stringOperator works as mathematical order. A different form of variable var tom = 5 + (9 + ‘  Cricket Games’);  output a string “59 Cricket games”   instead “14 Cricket Games”  because there is a parenthesis pair taking place in operation.  If string comes first, this (+) operator perform only string concatenation even there is a number after it.

This will return a string Cricket Games 59. I hope you already figured out different form of + operator. Now let’s talk about subtraction (-) operator, It performs same as like the addition (+) operator.

But subtraction operator can’t be used with string same like  as + operator.   Division and multiplication operator shown below.

Another arithmetic operator is modulus ( % ), it returns the reminder of a division.  But be careful about negative value while using modulus , output result also return in negative form.

Did we miss anything? Yes it’s the assignment ( = ) operator. We already used it several times without any notice. It usually assign value to a variable like var tom =20. Enough for today? Have a little break now ,we’ll learn conditional and logical operator with control structure in future.


Tags: