
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Why in JavaScript, “if ('0' == false)” is equal to false whereas it gives true in “if(0)” statement?
Let’s see the conditions one by one −
if(‘0’ == false)
It follows the following rule −
If Type(y) is Boolean, return the result of the comparison x == ToNumber(y)
The == does type coercion. This means an explicit type conversion is requested to match the type of the two operands. The left side '0' is converted to a number 0. On comparing the two numbers, and since 0 equals 0, the result is true. In this case, this does not work since it does not implies about the truish/falsy nature of the '0' string, since it got coerced before it was compared.
if(0)
This checks for the string to be null or empty, not whether it's zero or not. Always remember, a non-empty string is true. No type coercion is used here since strings can be evaluated as truish or falsy on their own merits.
- Related Articles
- If ([] == false) is true, why does ([] || true) result in []? - JavaScript
- Display TRUE FALSE records as 0 1 in MySQL
- Magnesium is a non-combustible substance. (True or False and if it is false give reason).
- State whether the following statement is true or false.'0' is a solution to the equation $x+1=0$.
- Smallest integers is zero. Is this statement true or false?
- If a number is divisible by 3 it must be divisible by 9. (True/False)
- Why does MySQL evaluate “TRUE or TRUE and FALSE” to true?
- Why does php's in_array return true if passed a 0?
- State whether the following statements are true or false. In case a statement is false, write the corrected statement in your notebook.(a) Cutting a log of wood into pieces is a chemical change. (True/ False)(b) Formation of manure from leaves is a physical change. (True/ False)(c) Iron pipes coated with zinc do not get rusted easily. (True/ False)(d) Iron and rust are the same substances. (True/ False)(e) Condensation of steam is not a chemical change. (True/ False)
- Check if it is possible to move from (0, 0) to (x, y) in N steps in Python
- Method to check if array element contains a false value in JavaScript?
- Which of the following statements are true (T) and which are false (F)?In any quadrilateral, if a pair of opposite sides is equal, it is a parallelogram.
- The area of the velocity time graph gives displacement of the body." State whether it is true or false."
- Is the following statement true or false :A rocket can propel itself in a vacuum.
- Return true or false in a MySQL select if another field contains a string?

Advertisements