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

  • Home
  • SEARCH
  • 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 6636177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:09:11+00:00 2026-05-25T23:09:11+00:00

The code: var num = 20; if(num == 20) { alert(It works); } else

  • 0

The code:

var num = 20;

if(num == "20")
{
    alert("It works");
}
else
{
    alert("Not working");
}

The question:

  1. In C programming we have a rule name data type promotion, where when there’s a mix of data type (example: addition of integer and floating point), the integer will first converted to floating point before the addition is being carry out.

  2. The code above will prompt me an alert box with the message "It works" that shows the if test condition is evaluate to true.

  3. For loosely typed JavaScript, I’m just curious: is there any rule like C that determines which conversion will be carry out in which situation? Besides that, the JavaScript code above converts num variable value from an integer value to string value before making comparison or vice versa?

  • 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-05-25T23:09:11+00:00Added an answer on May 25, 2026 at 11:09 pm

    Yes, all the rules of type conversion applied by the equals operator are described on the ECMA-262 specification, in The Abstract Equality Comparison Algorithm.

    The algorithm might look quite complex but it can be summarized to the following cases:

    1. The type the two operands is the same:

      • For primitives (String, Number, Boolean, Null, Undefined)
        • Return true if the value is exactly the same
      • For the Object type
        • Return true if the two references point to the same object
    2. If the types of the two operands differ

      • If the type of one operand is either Null or Undefined
        • Return true only if the other operand value is either null or undefined
      • If one of the operands is of type Boolean or Number
        • (after some steps) Convert the other operand to Number and compare
    3. If one of the operands is an Object and the other is a primitive

      • Perform Object-to-Primitive conversion on the Object and compare again

    The Object-to-Primitive conversion is made through an abstract operation called ToPrimitive, this method will try to convert the object to a primitive value, using the internal [[PrimitiveValue]] method.

    This will try to ejecute the object’s valueOf and toString methods, and it will take the value of the first that returns a primitive value.

    In the case those two methods don’t return a primitive, or they aren’t callable, a TypeError is thrown, e.g.:

    1 == { toString:null } // TypeError!
    

    The above statement will produce a TypeError because the default Object.prototype.valueOf method doesn’t do anything more than actually the same object instance (this, not a primitive value) and we are setting an own toString property that’s not a function.

    A friend made small tool that might be interesting to you, it shows all the steps and recursive comparisons made between types:

    • JS Coercion Tool
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine I have this code: var myFunc1 = function(event) { alert(1); } var myFunc2
can i ask you guys a question? Below is my code: var num =
I got this code: function zeroPad(num, places) { var zero = places - num.toString().length
This code var offset = $('#snapwrap_inner').offset(); alert(offset.left) in FF returns 0px but in Opera
I have the following code: var sl: THashedStringList; begin sl:= THashedStringList.Create; sl.Duplicates := dupIgnore;
See this code: var jsonString = '{id:714341252076979033,type:FUZZY}'; var jsonParsed = JSON.parse(jsonString); console.log(jsonString, jsonParsed); When
I had this code : function (i) { alert(i); }(3); And it wasn't working
I have javascript function return array like this: <script type=text/javascript> function looping() { var
I have written a javascript to generate textbox dynamically. Code: function addtextbox(val) { var
I have this code: function addFormControls() { var e = document.getElementById(ProductsList); var prodid =

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.