Casting to string in JavaScript

前端 未结 8 1696
轻奢々
轻奢々 2020-12-04 07:59

I found three ways to cast a variable to String in JavaScript.
I searched for those three options in the jQuery source code, and they are all in use

相关标签:
8条回答
  • 2020-12-04 08:24

    According to this JSPerf test, they differ in speed. But unless you're going to use them in huge amounts, any of them should perform fine.

    For completeness: As asawyer already mentioned, you can also use the .toString() method.

    0 讨论(0)
  • 2020-12-04 08:31

    if you are ok with null, undefined, NaN, 0, and false all casting to '' then (s ? s+'' : '') is faster.

    see http://jsperf.com/cast-to-string/8

    note - there are significant differences across browsers at this time.

    0 讨论(0)
  • 2020-12-04 08:32

    Real world example: I've got a log function that can be called with an arbitrary number of parameters: log("foo is {} and bar is {}", param1, param2). If a DEBUG flag is set to true, the brackets get replaced by the given parameters and the string is passed to console.log(msg). Parameters can and will be Strings, Numbers and whatever may be returned by JSON / AJAX calls, maybe even null.

    • arguments[i].toString() is not an option, because of possible null values (see Connell Watkins answer)
    • JSLint will complain about arguments[i] + "". This may or may not influence a decision on what to use. Some folks strictly adhere to JSLint.
    • In some browsers, concatenating empty strings is a little faster than using string function or string constructor (see JSPerf test in Sammys S. answer). In Opera 12 and Firefox 19, concatenating empty strings is rediculously faster (95% in Firefox 19) - or at least JSPerf says so.
    0 讨论(0)
  • 2020-12-04 08:36

    There are differences, but they are probably not relevant to your question. For example, the toString prototype does not exist on undefined variables, but you can cast undefined to a string using the other two methods:

    ​var foo;
    
    ​var myString1 = String(foo); // "undefined" as a string
    
    var myString2 = foo + ''; // "undefined" as a string
    
    var myString3 = foo.toString(); // throws an exception
    

    http://jsfiddle.net/f8YwA/

    0 讨论(0)
  • 2020-12-04 08:37

    They do behave differently when the value is null.

    • null.toString() throws an error - Cannot call method 'toString' of null
    • String(null) returns - "null"
    • null + "" also returns - "null"

    Very similar behaviour happens if value is undefined (see jbabey's answer).

    Other than that, there is a negligible performance difference, which, unless you're using them in huge loops, isn't worth worrying about.

    0 讨论(0)
  • 2020-12-04 08:42

    They behave the same but toString also provides a way to convert a number binary, octal, or hexadecimal strings:

    Example:

    var a = (50274).toString(16)  // "c462"
    var b = (76).toString(8)      // "114"
    var c = (7623).toString(36)   // "5vr"
    var d = (100).toString(2)     // "1100100"
    
    0 讨论(0)
提交回复
热议问题