There are a lot of interesting facts that are still unknown and make the Javascript the world's most confusing language. I've tried to explain a few of them if they can help you in better understanding of the JS code.
2. Introduction
Why I’ve named this presentation as “Interesting Facts About Javascript” because
there is a whole lot of things we still do accidently in javascript without actually
knowing that how it works.
This presentation will literally break a lot of misconceptions about Javascript
behaviour you might have put in your mind.
So without wasting anymore time, let's quickly jump to the very basic javascript
fundamental thing types.
3. Data Types In Javascript
Many of you might have worked a little (may be in college time) in statically typed
language like “C” and here you are working in Javascript as a dynamically typed
language. So what is statically typed and dynamically typed language?
In statically typed language, each variable you declare must have a type
associated with it so that the compiler can allocate the proper memory for it.
But in dynamically typed language, the variables don’t have any type to be
associated to it. Did you ever declared a variable as integer, string or boolean??
So javascript is dynamically typed language. But why the hell we have some data
types there in Javascript????
4. Data Types In Javascript Contd….
As we have seen in our Javascript code that both we and the javascript engine
treat a value 30 differently than “30”. We consider 30 as a number but “30” as a
string.
What does it mean?
The above thing means that the value may have a specific type associated to it.
Value has a type or Variable??? Confused???
In javascript or any dynamically typed language, the variables are not associated
with any type. Remember, the values have type not the variables.
Means, var a = 5; means a is not type of “number” but the value in it is type of
“number”.
5. Value Types In Javascript
So now on, we will say it Value Types not Data Types. Basically there are 7 built-
in types there in Javascript.
1. null
2. undefined
3. boolean
4. number
5. string
6. object
7. symbol (in ES6)
All of the above types except object are primitive that means, they are passed by
value not reference. Objects are non-primitive values which are passed through
reference.
6. “typeof” Keyword In Javascript
How do you check that what typeof value a variable is holding? The very useful keyword typeof does this
for you. Whenever you check a value type, it will give you a string value out of the following 7 string
values.
In the above code, there are 8 lines but you can see that only 7 distinctly types are there. It means these
are the seven types you will get in a typeof expression. Here are two strange things we can see:
1. typeof null returns object while it is not object. How null can be an object type value.
2. typeof function returns function while we have talked many times that functions are objects.
7. Why typeof null Is Object??
Nothing to be surprised here. It's a 20 years old BUG in Javascript. Yes its a
bug that could not be fixed due to already having a large codebase on internet that
is dependant on typeof null === “object” expression.
Otherwise what we expect from the browser is typeof null should give us “null” as
string value.
If you want to check if variable has a null value type, check with the following
ways.
1. if(variable === null)
2. if(!variable && typeof variable == “object”). Null is the only falsy value, which
is false but still type of object.
8. “function” Type Value
In Javascript, we have seen that functions are just objects which can have static
properties on them. But they have been provided a specific type called
“function”.
Functions are kind of callable objects and have special place in Javascript
environment.
9. Undefined VS. Undeclared - Most Confusing
Variables which don’t have any value will have a specific hidden value called undefined. You can check it
with typeof keyword like below:
A lot of times, we see undefined and undeclared as same things but they don’t actually. An undefined
variable is that which has been declared but not assigned any kind of value. Where an undeclared
variable is that which has never been even declared in script.
Here the most confusing part is that error message. Don’t you see “ReferenceError b is not defined” is
exactly like “b is undefined”. But remember, undefined and is not defined are two completely different
things in Javascript.
10. Undefined VS. Undeclared - Continued….
It could be very nice if the javascript engine could tell us that ReferenceError: b is not found if the
variable is never declared. At Least it makes some sense.
One more confusing part:
Here you can see that if we put typeof on both the undefined and undeclared variables, it will return the
type value “undefined”. It means, typeof has a special behaviour that gives us flexibility to check whether
a variable is declared or not. It won’t throw error if the variable is not declared.
The most common use of typeof is to check if we want to check any third party library global object like
typeof jQuery !== ‘undefined’ etc. It won’t throw error even if you have not included jQuery in your script.
11. Arrays In Javascript
How many of you have enough understanding of Javascript Arrays?
Arrays are just a kind of container that hold any type of value. Whether it is string, number, boolean, object
or another array (Multidimensional).
You never need to define size of the array first. You can declare it and push values directly. For e.g.
12. Delete A Value From Array
Never get trapped by the keyword delete. Though it will delete the value from array but will not behave
exactly like you thought. For example:
Using keyword delete will delete the value but will not actually re-index the array. What it will do is just
make the slot empty. In the above example the slot at index 1 will actually have no value and that's why, it
show undefined in log. So even deleting all the value from array with delete keyword, it will not update the
length of the array.
Instead use array.splice(1,1) which will remove the value from index 1 to 1 number of values.