- Number (not Integer)
- null (Special value I would call it)
- undefined (Special value I would call it)
Let’s talk about each of this data type in detail:
Now as we know that they are floating point numbers we have to be bit careful about arithmetic operations over the operands of this type, especially if we are performing calculations representing money. The reason is, floating point operations always return approximations, not exact values. For example:
Now, we will see how to deal with this situation. Solution here is very simple. We know that when we are dealing with money, we deal with numbers like 34.56$ or 8.99$ or 100.35 INR. These are the numbers with precision up to 2 digits after floating point. So scale them up by multiplying with 100. Perform arithmetic operations and then again scale them back by diving with 100. Let’s apply this solution to function above and see what it returns:
Another important value to keep in mind is NaN while dealing with arithmetic operations. It is an output of any erroneous arithmetic calculation. If any arithmetic function is given NaN as an input, it is sure that end result will be NaN too.
It provides a function (or we can say constructor), Number(value) which helps converting a string into a Number. It works same a + prefix operator (we will see in the program).
The final thing we will discuss about Number is a function called parseInt (strange again, now it says int). Well, this function helps parsing a string into Number. It takes two arguments: parseInt(String to parse, raddix)
parseInt will stop parsing the input String when it will encounter the first no-numeric character. It is also important to pass the radix argument which we can see in the program above. It the input String is ’08’ in that case we can see the output is numeric value 7 because
parseInt thinks (in the absence of radix argument) that input String is an octal number input. In the third statement when we have passed raddix argument we can see that now the output is 8. The radix argument is nothing but the base into which we want to convert the String.
It is important to know as a developer that the default value of radix is not
10. It always depends on the first input. parstInt converts first arguments to string. If it can be parse the output will be an Integer other wise it will return
- Input string begins with
0x, the radix value will be assumed as 16
- Input string begins with
0, the radix value assumed is
10. Which will be used? It depends how browser has implement ECMA5 standards. How to cope with it? Always be specific and provide radix value.
- For any other input string, radix assumed is
There are two Boolean values, true and false. Function Boolean(value) takes value as an input and based on the value being truthy or falsy, it returns true or false. This urges us to delve in another concept which is: ‘What is truthy or falsy value?‘
- ” or “” (empty String)
- 0 (numeric value zero
Rest all values are truthy value. Even “0” (zero in quotes) or “false” are turthy values as all the non-empty String are truthy.
typeof null is
null is assigned by the programmers.
Well, it is a special value (I wish a less confusing name would have been choosen) assigned to any variable when when left uninitialized. It means a variable defined but not assigned any value will have value undefined in it. undefined == undefined returns true and type of undefined returns undefined when typeof function is used. undefined is also a value returned when we try to extract a member from an object when it doesn’t have a member present.
Rest everything in Java is an Object. But that will be an entirely a different discussion. We will take it separately in a different article.