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 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.
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.
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.
We 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.
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.
Operator 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.