I have seen when developers(having different language background), start working with javascript, generally do not have enough time to learn this wonderful scripting language. As a result they carry a lot of misconceptions about this language. I feel this is a big mistake, since this is going to hamper one in writing highly efficient javascript program also these misconceptions leave a chance of loopholes in the code.
Therefore I am going to dedicate some of my blogs writing about a few misconceptions which I have seen developers generally have.
Today I will be writing about the values of javascript which result in false when executed as an expression in if/else block.
This is an important piece of information that a javascript developer should always be aware of, because at times I have found that the intention of developers are to prohibit null or undefined values but they eventually disallow all the falsy values which may include number 0 or an empty string.
In most of the language, if condition always takes expressions which result in either true or false.
In javascript also, the if block is executed when the expression passed as the condition results in truthy value.
The below are falsy values in javacsript:
Therefore I am going to dedicate some of my blogs writing about a few misconceptions which I have seen developers generally have.
Today I will be writing about the values of javascript which result in false when executed as an expression in if/else block.
This is an important piece of information that a javascript developer should always be aware of, because at times I have found that the intention of developers are to prohibit null or undefined values but they eventually disallow all the falsy values which may include number 0 or an empty string.
In most of the language, if condition always takes expressions which result in either true or false.
In javascript also, the if block is executed when the expression passed as the condition results in truthy value.
The below are falsy values in javacsript:
false
null
undefined
The empty string ''
The number 0
The number NaN
null
undefined
The empty string ''
The number 0
The number NaN
The point to note down here is that all other values in javascript are truthy.
All objects including empty arrray[], strings(other than the empty string), all numbers (other than 0) even negative numbers will result in truthy value.
Check the below the below code:
All objects including empty arrray[], strings(other than the empty string), all numbers (other than 0) even negative numbers will result in truthy value.
Check the below the below code:
if ("false")
{
//this condition will always get executed
}
if (0)
{
//this condition will never get executed
}
{
//this condition will always get executed
}
if (0)
{
//this condition will never get executed
}