Are you know 'types' in JavaScript ?

Are you know 'types' in JavaScript ?

ยท

3 min read

Many other languages unlike JavaScript have their type, according to mostly developers . For those, who are fan of strongly typed language. In those languages, the variable that holds the value, have its own type ( like integer or character) but in JavaScript the value, that holds by the variable, have its type rather than the variable. The same variable can hold value that have a type and that same variable can hold different value that have different type in other statement. Let's have a look:

let a = 14;
typeof(a);  // 'number'

a='hello world!';
typeof(a);  // 'string'

type identifies the characteristics of a particular value and distinguish it from other values. Like, 14 and "14" looks like they are same value but they are different by their types number and string respectively.

If we do some arithmetic operation on both value like:

let a = '14';
let b = 14;
        a + b // '1414'

In above code, b will implicitly coerce(convert) into string and, it will concatenate with the value a (that is string type) with the help of + operator ( shorthand concatenation in JavaScript ).

The implicitly conversion may confuse most of the developers and it may cause bug or crashed your application, if you have not understanding with the JavaScript types. The conversion of types may be very useful feature for you, if have good understanding of types. For that, we should have better understanding on values and types.

Built-in Types:

  • Boolean
  • string
  • number
  • object
  • undefined
  • null
  • symbol
typeof true;             //'boolean'
typeof "hello world";    //'string'
typeof 14;               //'number'
typeof {a:1, b:2};       //'object'
typeof undefined;        //'undefined'
// symbol is added after es6
typeof Symbol();         //'symbol'

typeof null;             //'object'

Now you may be thinking that, why the null is returned the type object ?

Because, it is a bug that null returned type object and it persisted from many years ago. It will never be fixed because if it'll be fixed, many web application rely on that buggy thing, if it will be fixed than many web application will break.

undefined and undeclared

let a;
typeof a; //'undefined'

Declaring a variable that have not assign to any value. Calling typeof against such variable will give undefined. Because that is not defined yet. Do not mixup the undefined with the undeclared, they both are different. Undeclared is, when we do not declared the variable in our scope.

  c   //ReferenceError: c is not defined

c is not declared in scope and hence it throw an ReferenceError. But, when we calling typeof against such undeclared variable, it will give us undefined type rather that undeclared.

typeof c;  //'undefined'

What about arrays?

markus-winkler-wpOa2i3MUrY-unsplash.jpg

let arr=[1,2,3];
typeof arr;   //'object'

Why the arrays returned the type object. Are arrays object ? No, it's not the type object but, it's the sub-type of object with having numerically index positioning rather that the key value pair.

ย