Javascript - types

·

2 min read

ECMAScript is an organization that creates certain rules for all scripting language, it creates for JavaScript as well. In Javascript variables are not defined, values are defined. This is a stark contrast between js and other programming languages out there. So there is a misconception that js doesn't support datatypes, while it is wrong, it supports datatypes but not in variables but in its value. let a = 2; a="rahul";

This arises a problem that we might not always be able to see conversion of one type to another. In javascript conversion of one type to another type is termed as "coercion". It is mainly of two types Explicit and Implicit coercion. Explicit coercion is a coercion which we can see from the code, but implicit coercion is hard to notice just by looking at the code.

console.log(`To print a statement along with some ${variable}`)

When you compare two values, regardless what values you compare, the end result will always be a boolean value true or false. We use == and === to compare values. It is generally said that == checks for value, while === checks for value as well as type. This generalization is wrong, in reality == checks for value equality with coercion allowed, while on the other hand === checks for value equality without allowing value coercion.

So if you're confused what equality to use and when to use them, just keep in mind that if you are certain about values use == , and in case if you aren't use ===. And also keep in mind that != pairs with ==, while !== pairs with ===

Equality behavior in js is different for non-primitive data types(arrays, objects). Array's, object's values are stored by their reference. So the == === will check only their reference value, not their actual underlying value. Arrays are by default coerced to strings. For example A=[1,2,3] will be coerced into "1,2,3".

Another peculiarity in JS is the similarity between undefined and is not defined. For example let a; then typeof(a) yields undefined sounds great right? But if you write typeof(b) then the console will show ReferenceError: b is not defined . So you might think that you haven't defined type for variable b , but in reality you haven't even declared it. So keep in mind that undefined and is not defined are two different things in JavaScript.