2. JavaScript : Values & Types
Do you know, JavaScript have typed values instead of typed variables unlike other programming languages. Want to know, If JavaScript have Type Enforcement?
Dive Deep into this article to know how & why JavaScript have typed values. Also, get too know about built-in types in JavaScript, and whether Type Enforcement exists in JavaScript or not!
JavaScript has typed values, not typed variables. This just simply means, In JavaScript, variables don’t have types — values have types. Variables in JS are just simple containers for those values. (This you will see in below examples, what does this means 😃)
Below are the 7 built-in types available in JavaScript:
string
→ Set of characters or text.number
→ Integer or floating point number.boolean
→ True or False.null
→ Special keyword denoting null.undefined
→ Denoting whose value is not defined yet.object
→ compound value where you can set properties (named locations) that each hold their own values of any type. Objects are written in curly braces {} as name: “value” pairs, separated by commas.
var car = {Name : "Ford", Model : 2016, Color : "blue"}
symbol
(new to ES6) -> It has unique instances, which are immutable.
Note: All of these types except object
are called "primitives".
JavaScript has “
typeof”
operator, which helps us examine value & its type:
The typeof
operator always returns a string.
Examples illustrating identification of JS Data Types, using typeof
operator:
var a; // declaring JavaScript Variable
console.log(typeof a); // "undefined"
//(Because a is declared, but not yet initialized)
a = "42";
console.log(typeof a); // "string"
a = 1000;
console.log(typeof a); // "number"
a = false;
console.log(typeof a); // "boolean"
a = null;
console.log(typeof a); // "object"
// This is a long-standing bug in JS, returns object instead of null
a = undefined;
console.log(typeof a); // "undefined"
a = { car : "Ford" };
console.log((typeof a); // "object"
typeof Symbol() // "symbol"
Noticed how, typeof a
is not looking for "type ofa
", but rather it is looking for the "type of the value which currently resides in a."
This makes clear, why only values in JS has types & variables are just containers for those values 😀
💡Tip: “Undefined” & “UnDeclared” are Different
Many developers will assume “undefined” and “undeclared” are roughly same thing, but in JavaScript, they’re different.
undefined
-> A value that declared variable can hold.- "Undeclared" means variable has never been declared.
However, JavaScript unfortunately conflates these two terms at 2 places:
- In its error messages (“ReferenceError: b is not defined”)
- In return values of
typeof
, which is"undefined"
for both cases — variable declared but not intialized and variable not declared itself.
//Error Messages----------------------------------------------------
var a;
console.log(a); // "undefined"
console.log(b); // ReferenceError: b is not defined
// above Reference error doesn't means that b is undefined, here b is undeclared
// Type Of----------------------------------------------------------
var a;
console.log(typeof a); // "undefined"
console.log(typeof b); // "undefined"
//no error thrown when we executed typeof b, even though b is undeclared variable
Does JS have “Type Enforcement” ?
No. JavaScript doesn’t have Type Enforcement.
JS Engine doesn’t insist that variable always holds values of same initial type that it starts out with. Variable, in one assignment statement, can hold a string
, and in other hold a number
, and so on.
var a = 42;
console.log(typeof a); // "number"
a = true;
console.log(typeof a); // "boolean"
Console.log is used for demonstration purpose in the article, however it should be avoided in production code.
Happy JavaScript Learning & Coding!😀
Thanks for reading :) That’s it, I hope you liked the article.
Who am I to write?
Just a curious Learner, who loves to develop websites, and have developed few websites using various templates, using React (from scratch) as well, most of them as personal projects or college club sites, since past 3–4 years. In short, I have worked on React, JavaScript, CSS & HTML and still learning new things daily!
Feel free to reach out to me, if you want to discuss anything related.
I would be glad & happy if you send your valuable feedback & suggestions. Your suggestions really matter!!
You can contact me here -
LinkedIn: https://www.linkedin.com/in/itaniyagupta/
Previous Articles in the the series, access below👉 :
1. Introduction to JavaScript https://medium.com/@itaniyagupta/1-introduction-to-javascript-b518204f1fa6