locked
Javascript RRS feed

  • Question

  • User-328230518 posted

    var s = '5';
    var a = s*1;

    typeof(a); //"number"

    how the typeof(a) is number?

    Sunday, October 2, 2016 4:51 PM

Answers

  • User475983607 posted

    JavaScript implicitly convert types.

    https://www.united-coders.com/matthias-reuter/all-about-types-part-2/

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, October 2, 2016 6:51 PM
  • User-707554951 posted

    Hi helpneed,
    When JavaScript tries to operate on a "wrong" data type, it will try to convert the value to a "right" type. So, the variable (s) “ 5“  is convert to 5, and the  type of adding  result is a number.
    For the detailed information about JavaScript Type Conversion. You could refer to the following link:
    http://www.w3schools.com/js/js_type_conversion.asp
    Hope this can help you. If you have any question and confusion about the problem. Please don't hesitate to let me know.
    Best regards
    Cathy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 4, 2016 3:12 AM

All replies

  • User475983607 posted

    JavaScript implicitly convert types.

    https://www.united-coders.com/matthias-reuter/all-about-types-part-2/

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, October 2, 2016 6:51 PM
  • User-707554951 posted

    Hi helpneed,
    When JavaScript tries to operate on a "wrong" data type, it will try to convert the value to a "right" type. So, the variable (s) “ 5“  is convert to 5, and the  type of adding  result is a number.
    For the detailed information about JavaScript Type Conversion. You could refer to the following link:
    http://www.w3schools.com/js/js_type_conversion.asp
    Hope this can help you. If you have any question and confusion about the problem. Please don't hesitate to let me know.
    Best regards
    Cathy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 4, 2016 3:12 AM