Category Archives: Javascript

null >= 0

Javascript never stops surprising you.

Try this out.

then try

then once opon a time try this

What really happens is that the Greater-than-or-equal Operator (>=), performs type coercion (ToPrimitive), with a hint type of Number, actually all the relational operators have this behavior.

null is treated in a special way by the Equals Operator (==). In a brief, it only coerces to undefined:

Value such as false, ”, ‘0’, and [] are subject to numeric type coercion, all of them coerce to zero.

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Beautiful for loop

Have a look at below code, where I am traversing the list of elements and doing some operation on it

with every increment in loop, we are checking i < elements.length. I can store elements.length in some variable, which increases the performance by caching the value of length.

Isn't it better. How about not using the extra variable and still achieve the same.

Wonder!, this is cool

Facebooktwittergoogle_plusredditpinterestlinkedinmail

JavaScript Trick Questions

This is some of my collection of javascript trick questions. If you have more, please do comments.

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Making an array immutable

Let’s say, I have a requirement of making an array immutable(no one can modify). Usually, when passing an array to a function as an input, it is passed by reference, not by value. So the function can change or alter its value instead of just using it.

A simple trick would be to freeze the array using Object.freeze

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Color code validator without Regular Expression

Hi Guys, My friend gave me this idea about validating color code. I can write a regular expression but need to write for Hex Color, RGB, and color name as well, which is very difficult.

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Two versions of IIFE

Hello Friends,

There are two version of IIFE(immediately found executable function) which our fellow developers uses.

other is

I don’t know which one is better, but you might found other ways to do that.

Facebooktwittergoogle_plusredditpinterestlinkedinmail

setAttribute & removeAttribute with checkbox

Let say, we have a checkbox and button to toggle on/off for checkbox

then some javascript for click event of button

Now if you click on the button, you can see, it is working fine, but, the big “BUT”,
click on the checkbox first and then click on the button. !Its not working any more! as we expected.

I don’t know the reason why it is not working, somehow, I know how to fix it.
So instead of setAttribute and removeAttribute, use something like

Facebooktwittergoogle_plusredditpinterestlinkedinmail