Skip to main content


Showing posts with the label falsy

Falsy JavaScript

In continuation of my previous blog on the basic JavaScript expressions, this blog is about Falsy and Truthy values in JavaScript.

When presented with a non boolean expression instead of a boolean value, JavaScript uses Truthy or Falsy values instead to determine the result. At first glance, this could be confusing and could lead to errors while scripting.

var a = "someval";
var b;
if (a) { // this is a truthy
 // Block is executed
if (b) { // b is undefined and so a falsy
// Block is not executed

The Falsy Values
All expressions, objects and values are Truthy by default. There are exceptions and they are called the Falsy values and are listed below:
  # The empty string; ""
  # null
  # undefined
  # The number 0
  # NaN
  # The boolean false

It gets tricky
If you see the below table, you will note that it gets tricky or rather one should be a little extra careful while dealing with Falsy values.

ExpressionValueFalsy ValuesfalsefalseNaNfalseundefinedfalse0fa…

JavaScript - Undefined Infinity or Not a Number?

Had a heated discussion with a friend about a particular expression and so this blog with ready references for some basic JavaScript expressions.

ExpressionValueInfinityAnything beyond +/-1.7976931348623157e+308typeof Infinity"number"typeof NaN"number"typeof undefined"undefined"typeof null"object"Infinity + InfinityInfinityInfinity - InfinityNaN-Infinity + InfinityNaNInfinity / InfinityNaNInfinity * InfinityInfinityInfinity * 1InfinityInfinity / 1InfinityInfinity / 0InfinityInfinity * 0NaNInfinity - 1e308InfinityInfinity - 1e309NaN-Infinity + 1e308-Infinity-Infinity + 1e309NaNInfinity / 1e308Infinity1 * "a"NaN1 + NaNNaN1 * NaNNaNundefined + 1NaNundefined * 1NaNundefined + InfinityNaNundefined * InfinityNaNundefined + NaNNaNundefined * NaNNaNundefined + nullNaNundefined * nullNaN1 + null11 * null0null + null0null * null0Infinity + nullInfinityInfinity * nullNaNNaN + nullNaNNaN * nullNaN"" + Infinity"Infinity""&q…