{ Fun Facts of JavaScript }
Earlier back in 90's, pages displayed on browser has to be static and designed with basic HTML and CSS. Java Applets were complete failure at client side, then came JavaScript that was created in 10 days. Looking at its tremendous performance on client side it was adopted in early stages as client side scripting language and not given a chance to get mature. This is the reason why JavaScript has lot of bugs. This post contains some major issues in the language and reason behind those design decisions.
NaN === NaN // Returns False
Typeof null === Object
Earlier back in 90's, pages displayed on browser has to be static and designed with basic HTML and CSS. Java Applets were complete failure at client side, then came JavaScript that was created in 10 days. Looking at its tremendous performance on client side it was adopted in early stages as client side scripting language and not given a chance to get mature. This is the reason why JavaScript has lot of bugs. This post contains some major issues in the language and reason behind those design decisions.
NaN === NaN // Returns False
undefined === undefined // Says True
What is wrong with NaN?
Actually JavaScript or say ECMAScript follows IEEE-754 standard which purely says NaNs should never be equal.
NaN
is designed to bubble up all the calculations flaws buried deep down in
your complex code. If NaN is kept with any operator then it will break
the calculation by indicating meaningful results. Otherwise by identity
NaN/NaN should equal 1, (NaN/NaN)==1, (NaN*1)==NaN, etc which surely we
do not want.
Functions can be nested
Well,
if you compare JavaScript with more strictly typed languages like Java
or C# then you will find lot of new things to deal with or to remember.
So first thing first, stop comparing any Loosely typed language with
Strongly typed Language. Now open your mind for some new, amazing
features of Loosely typed Language.
var AnikenFeatures = function () {var LukeFeatures = function() {return “Force!!”}return “Dark side of Force!!”}
Functions
are another type of variable in JavaScript. When we create a function
within another function then it changes the scope of the function in
same way as it changes the scope of variable. Main advantage: Closures, which results in reducing total global namespace pollution.
Functions can execute themselves
(function() { console.log('Valar Morghulis'); })(); // Says Valar Morghulis
This
anonymous function will execute itself without need of any object or
context. So if we are not giving context and it is automatically getting
executed then what would be the default this of the executed function. Let’s check:
(function() {console.log(this); // Says Window Object})();
Above code will return Window object, i.e. default context/this of each executed function will be Window Object.
But
what is the purpose of self executing function. It’s nothing but for
variable scoping. Variables declared and defined within self executing
function can only be accessed or available in that function. Developer
need not have to worry about the variables named in other JavaScript code block.
Main
benefit lies when your application contains lot of external JavaScript
libraries. There is fair chance of getting same name for more than one
function or variable. In that case, library loaded later will override
the definition of function/variable of previous library.
Complete code of JQuery library is wrapped inside the self executing function because of the above mentioned reasons.
Defining Undefined
var a;if(a === undefined){console.log(“This is undefined”)} // Says This is undefined
Default
value of each variable if not defined is - undefined. This can be used as
trick in many situations. But mainly to check if an object/variable
contains any method/value.
JQuery Code:
(function(Global) {Global.foo = {};})(typeof window !== “undefined” ? window : this)
Here, in this self executing function, parameter is passed by checking weather window object is available or not. If not then this will be transferred in place of window object. Well in that case value of this would be the object which is invoking the function.
Typeof null === Object
console.log(typeof null === object) // It says true!!
Why in the name of god it returns true. Because Null in JavaScript is Object. You ask why? Here is fun fact:
This is a bug and one that unfortunately can’t be fixed, because it would break existing code. Let’s explore the history of this bug.
This bug exists from the first version of JavaScript where values were represented as a type tag and a value, with the type tag for objects being 0, and null was represented as the NULL pointer (0x00 on most platforms). As a result, null had 0 as a type tag, hence the bogus typeof return value.
A "fix" was proposed for ECMAScript (via an opt-in). It would have resulted in:
typeof null === 'null'
... but this change was rejected, due to issues with code using this specific "quirk" to test for null.
0.2 + 0.1 == 0.3 // Returns False
Whaattt!!!!!!!!
Because internally, computers use a format (binary floating-point) that cannot accurately represent a number like 0.1, 0.2 or 0.3 at all.
When the code is compiled or interpreted, your “0.1” is already rounded to the nearest number in that format, which results in a small rounding error even before the calculation happens.
You ask why do computers use such a stupid system?
It’s not stupid, just different. Decimal numbers cannot accurately represent a number like 1/3, so you have to round to something like 0.33 - and you don’t expect 0.33 + 0.33 + 0.33 to add up to 1, either - do you?
No comments:
Post a Comment