JavaScript 101: Data Types


The Head First JavaScript book says ..
…but to truly master the language, get that promotion, and get on to the things you really want to do in life, you have to rock at types.
I guess that’s what I really want to do .. So better study the chapter on Types seriously.

Null, Undefined, NaN:
This is a very confusing topic, even after so many years of programming I can’t wrap it up around my head! So in a nutshell -
Null is used to represent “no object”. That is, it is used in place where an object should be there but it isn’t. Like getElementById returns null when it does not find an object with that ID.
Undefined, on the other hand, is used to represent a variable that has not been initialized, an object with a missing property, or an array with a missing value.
NaN is a special value in JS that is used to represent a number that can’t be represented in computers. One example is (0/0). NaN != NaN, because the two numbers which can’t be represented may no necessarily be equal. IsNaN() is a special function used to check whether or not an expression evaluates to NaN.
Type of NaN is : number
Type of undefined is : undefined
Type of null is: object

Equality Checking and Type Conversion:
JavaScript has the == operator to check for equality. This operator works as follows:
  1. If the types of two operands are same, compare them.
  2. If the types of two operands are different, try to convert them to same types and then compare them.
It uses the following rules to convert operands:
  1. When comparing a string and number, convert the string to a number. If it cannot be converted, it is a NaN.
  2. When converting any value to boolean, convert both of them to numbers. (False = 0, True = 1) and compare.
  3. Comparing null and undefined always yields true.
Type Conversion for + operator:
If both operands are numbers, add. Else, if one is a string, concat them.

Type Conversion for other operators:
Try to convert both operands to numbers and then evaluate -, * or /.

The strict equality operator:
JavaScript uses the operator === to ensure strict comparison. This operator works just like == operator of any strictly typed language: if both operands ain’t of the same type, they ain’t equal.

Checking whether objects are equal:
Both == and === behave exactly same when comparing object references. They both compare references, which means the variables containing the addresses. So obj1 == obj2 returns true, if and only if obj1 and obj2 point to the same object.

Truthy and Falsy values:
These values are those which are not boolean but which are considered to be “true” or “false” by JavaScript, when comparing. There are 5 falsy values, rest all are truthy:
  1. null
  2. undefined
  3. “”
  4. 0
  5. NaN
A closer look at strings:
JavaScript has two types of strings: primitive and object strings. When we create a string, by default it is primitive. Such a string does not have any functions or properties. However, whenever we call a function on a string (such as substring) or access it’s properties, the primitive is converted to an object temporarily (which now has methods and properties) and then operation is performed on them.

No comments:

Post a Comment