In JavaScript, why is "0" equal to false, but when tested by 'if' it is not false by itself?

meaning in javascript
... in javascript es6
in javascript
in javascript
in javascript
... in javascript array
javascript ... operator three dots
javascript ... notation

The following shows that "0" is false in Javascript:

>>> "0" == false

>>> false == "0"

So why does the following print "ha"?

>>> if ("0") console.log("ha")

The reason is because when you explicitly do "0" == false, both sides are being converted to numbers, and then the comparison is performed.

When you do: if ("0") console.log("ha"), the string value is being tested. Any non-empty string is true, while an empty string is false.

Equal (==)

If the two operands are not of the same type, JavaScript converts the operands then applies strict comparison. If either operand is a number or a boolean, the operands are converted to numbers if possible; else if either operand is a string, the other operand is converted to a string if possible. If both operands are objects, then JavaScript compares internal references which are equal when operands refer to the same object in memory.

(From Comparison Operators in Mozilla Developer Network)

What is JavaScript Used For?, this keyword refers to the object it belongs to. In a function, this refers to the global object. In a function, in strict mode, this is undefined . In an event, this refers to the element that received the event. JavaScript is a programming language used primarily by Web browsers to create a dynamic and interactive experience for the user. Most of the functions and applications that make the Internet indispensable to modern life are coded in some form of JavaScript. The earliest incarnations of JavaScript were developed in the late 1990s for the Netscape Navigator Web browser.

Tables displaying the issue:

and ==

Moral of the story use ===

table generation credit:

What does the operator !== mean?, operator is the negation of === , which tests for equality without performing implicit type conversions, according to the "Strict Equality Comparison Algorithm"[1]. This kind of equality is called strict because it doesn't make exceptions for values that are "almost" equal. JavaScript Enhances Your Web page In all appropriate uses of JavaScript, the purpose of the JavaScript is to enhance the way the web page works and to provide those of your visitors who have JavaScript enabled with a friendlier site than is possible without the JavaScript.

It's according to spec.

12.5 The if Statement 

2. If ToBoolean(GetValue(exprRef)) is true, then 
a. Return the result of evaluating the first Statement. 
3. Else, 

ToBoolean, according to the spec, is

The abstract operation ToBoolean converts its argument to a value of type Boolean according to Table 11:

And that table says this about strings:

The result is false if the argument is the empty String (its length is zero); otherwise the result is true

Now, to explain why "0" == false you should read the equality operator, which states it gets its value from the abstract operation GetValue(lref) matches the same for the right-side.

Which describes this relevant part as:

if IsPropertyReference(V), then 
a. If HasPrimitiveBase(V) is false, then let get be the [[Get]] internal method of base, otherwise let get
be the special [[Get]] internal method defined below. 
b. Return the result of calling the get internal method using base as its this value, and passing 
GetReferencedName(V) for the argument

Or in other words, a string has a primitive base, which calls back the internal get method and ends up looking false.

If you want to evaluate things using the GetValue operation use ==, if you want to evaluate using the ToBoolean, use === (also known as the "strict" equality operator)

Assignment operators are used to assign values to JavaScript variables. Given that x = 10 and y = 5, the table below explains the assignment operators: Operator  JavaScript is mainly used for frontend development, and there are various tools that will allow you to quickly get a working prototype with JavaScript. Nonetheless, you should first focus on obtaining a solid understanding of JavaScript before you explore what the community has to offer.

It's PHP where the string "0" is falsy (false-when-used-in-boolean-context). In JavaScript, all non-empty strings are truthy.

The trick is that == against a boolean doesn't evaluate in a boolean context, it converts to number, and in the case of strings that's done by parsing as decimal. So you get Number 0 instead of the truthiness boolean true.

This is a really poor bit of language design and it's one of the reasons we try not to use the unfortunate == operator. Use === instead.

JavaScript is a programming language used primarily by Web browsers to create a dynamic and interactive experience for the user. Most of the functions and  Maybe it’s not engaging enough or it’s not as creative as you want it to be. JavaScript is that missing piece which can be used to enhance web pages, applications, etc to provide a more user-friendly experience. JavaScript is the language of the web, it is used to make the web look alive by adding motion to it.

// I usually do this:

x = "0" ;

if (!!+x) console.log('I am true');
else      console.log('I am false');

// Essentially converting string to integer and then boolean.

While JavaScript is a fun and powerful language, it can be tricky and requires a proper understanding of its underlying principles to mitigate  Nowadays almost all web pages contain JavaScript, a scripting programming language that runs on visitor's web browser. It makes web pages functional for specific purposes and if disabled for some reason, the content or the functionality of the web page can be limited or unavailable.

Closures aren't a JavaScript-only concept, although if you're part of the JavaScript/Node.js community, you might've heard the term quite a bit. Many Internet Web sites contain JavaScript, a scripting programming language that runs on the web browser to make specific features on the web page functional. If JavaScript has been disabled within your browser, the content or the functionality of the web page can be limited or unavailable.

In other words, every javascript function while executing has a reference to its current execution context, called this. Execution context means  The reason why the == operator reasons that "3" and 3 are the same is because it actually coverts the operands (the values either side of the == operator) to the same type before it does the comparison. However, if we change the operator to an identity operator, as shown here, we see quite different output:

There are essentially two types of values in JavaScript. The first type is primitives, and the second type is objects (which also includes functions). The dollar sign $ was chosen for the function name by the first of these libraries because it is a short one-character word, and $ was least likely to be used by itself as a function name and therefore the least likely to clash with other code in the page.

  • "0" is a string, and since it's not empty, it's evaluated to true.
  • "0" === false [...] false
  • Check out Angus Croll's article truth in javascript.…
  • '0'==false but '0' is not a falsey value (yes Javascript can be weird)
  • @Linsey: The whole "falsy" and "truthy" thing was only ever meant to explain how values are converted to booleans. When you compare two values with ==, they never get converted to booleans, so it doesn't apply. (The rules for the conversion seem to favour converting to numbers.)
  • It makes much more sense with another order of values
  • From now on, if someone says that he never uses strict comparison operators, I will face him with these tables and make him cry. Still not sure if I grasp the concept of NaN though. I mean, typeof NaN // number but NaN === NaN // false, hmm...
  • A friend of mine made - the same graphs as above, but a bit easier to read.
  • @JustusRomijn there are multiple values to represent NaN, so when you are comparing 2 NaNs, they are of different values (I guess). Read the first quote here.
  • These tables have a mistake. Neither == nor === operator for the [], {}, [[]], [0] and [1] values do not evaluate to true. I mean [] == [] and [] === [] also false.
  • "a string has a primitive base, which calls back the internal get method and ends up looking false" Is this true for all strings ?
  • @Interstellar_Coder Section 8.12.3: [[Get]] (P) describes how it works. It's true only for cases that string are 0, as it does a bunch of other internal calls eventually resulting in GetOwnProperty which sees that "whatever" is a data property, which then returns all the way back that value. This is why "0" is false, and "blah" is true. Check out some of Douglas Crockford's videos on Yahoo developer theater, he describes "truthyness" in JavaScript a little less complex than I am. If you understand what "truthy" and "falsy" means you'll understand Bobince's answer right away.
  • Where can I find the spec?
  • correct, not about how to "make it work" but the question is more like, "why it behaved that way?"
  • I didn't know someone ported the spec to a site... that's awesome! No more PDFs for me.