Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7876209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:07:58+00:00 2026-06-03T03:07:58+00:00

Today when I was doing some experiments with == , I accidentally found out

  • 0

Today when I was doing some experiments with ==, I accidentally found out that "\n\t\r" == 0. How on earth does "\n\t\r" equal to 0, or false?

What I did is:

var txt = "\n";  //new line
txt == 0;        //it gives me true

And that really annoy me. So I did more:

var txt = "\r";  //"return"
txt == 0;        //true

var txt = "\t";  //"tab"
txt == 0;        //true

It does not make sense, at all. How’s that happen? And more crazy is this:

//Checking for variable declared or not

var txt ="\n\t\r";
if(txt!=false){
    console.log("Variable is declared.");
}else{
    console.log("Variable is not declared.");
}

What it gives me is Variable is not declared.

How is it equal to 0, or false???

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T03:07:59+00:00Added an answer on June 3, 2026 at 3:07 am

    This behaviour might be surprising but can be explained by having a look at the specification.

    We have to look at the what happens when a comparison with the equals operator is performed. The exact algorithm is defined in section 11.9.3.

    I built a simple tool to demonstrate which algorithm steps are executed: https://felix-kling.de/js-loose-comparison/


    string == integer

    The step we have to look at is #5:

    5. If Type(x) is String and Type(y) is Number,
    return the result of the comparison ToNumber(x) == y.

    That means the string "\n" ("\r", "\t") is converted to a number first and then compared against 0.

    How is a string converted to a number? This is explained in section 9.3.1. In short, we have:

    The MV (mathematical value) of StringNumericLiteral ::: StrWhiteSpace is 0.

    where StrWhiteSpace is defined as

    StrWhiteSpace :::
        StrWhiteSpaceChar StrWhiteSpace_opt
    
    StrWhiteSpaceChar :::
        WhiteSpace
        LineTerminator
    

    This just means that the numerical value of strings containing white space characters and/or a line terminator is 0.
    Which characters are considered as white space characters is defined in section 7.3.


    string == boolean

    The step we have to look at is #7:

    7. If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).

    How booleans are converted to numbers is pretty simple: true becomes 1 and false becomes 0.

    Afterwards we are comparing a string against a number, which is explained above.


    As others have mentioned, strict comparison (===) can be used to avoid this "problem". Actually you should only be using the normal comparison if you know what you are doing and want this behaviour.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

today i was doing some java script coding, in that i need some images
I unfortunately was doing a little code archeology today (while refactoring out some old
I have been doing a bit of testing with images today and found that
When doing some web maintenance today, I noticed a strange new folder on my
I was working on my advanced calculus homework today and we're doing some iteration
Recently I'm doing some work on RTMP streaming, that is using Flowplayer to integrate
Today I had an epiphany, and it was that I was doing everything wrong.
Ive been doing some research today on when an how to use the using
I was doing some testing and debugging of my Iphone app today and encountered
Today I was doing some leisurely reading and stumbled upon Section 5.8 (on page

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.