How to concatenate two numbers in javascript?

后端 未结 16 2352
傲寒
傲寒 2020-12-13 16:33

I\'d like for something like 5 + 6 to return \"56\" instead of 11.

相关标签:
16条回答
  • 2020-12-13 17:23
    enter code here
    var a = 9821099923;
    var b = 91;
    alert ("" + b + a); 
    // after concating , result is 919821099923 but its is now converted 
    into string  
    
    console.log(Number.isInteger("" + b + a))  // false
    
     you have to do something like below:- 
    
    var c= parseInt("" + b + a)
    console.log(c); // 919821099923
    console.log(Number.isInteger(c)) // true
    
    0 讨论(0)
  • 2020-12-13 17:26

    Use

    var value = "" + 5 + 6;
    

    to force it to strings.

    0 讨论(0)
  • 2020-12-13 17:31

    You can return a number by using this trick:
    not recommended

    [a] + b - 0
    

    Example :

    let output = [5] + 6 - 0;
    console.log(output); // 56
    console.log(typeof output); // number
    
    0 讨论(0)
  • 2020-12-13 17:34
    var value = "" + 5 + 6;
    alert(value);
    
    0 讨论(0)
提交回复
热议问题