最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Spread operator for strings - Stack Overflow

programmeradmin2浏览0评论

I read about spread syntax on MDN and that it can be used with both arrays and strings:

Spread syntax allows an iterable such as an array expression or string to be expanded in places where zero or more arguments (for function calls) are expected - mdn.

It's clear for me with arrays. It will expand the elements as separate arguments.
But I didn't find examples for strings.

So, what are the rules to use spread syntax to expand a string in a function call?
Should the string characters be separated with spaces cause I tried this and it printed 3.

var x = "1 2 3";
console.log(Math.max(...x));

I read about spread syntax on MDN and that it can be used with both arrays and strings:

Spread syntax allows an iterable such as an array expression or string to be expanded in places where zero or more arguments (for function calls) are expected - mdn.

It's clear for me with arrays. It will expand the elements as separate arguments.
But I didn't find examples for strings.

So, what are the rules to use spread syntax to expand a string in a function call?
Should the string characters be separated with spaces cause I tried this and it printed 3.

var x = "1 2 3";
console.log(Math.max(...x));

Share Improve this question edited Jan 7, 2018 at 18:31 Moaaz Bhnas asked Jan 7, 2018 at 18:23 Moaaz BhnasMoaaz Bhnas 1,1707 gold badges20 silver badges41 bronze badges 6
  • 4 Please be aware that MDN is not the JavaScript specification. – zzzzBov Commented Jan 7, 2018 at 18:26
  • 2 Why wouldn't it print 3, max operates on numbers so it is going to try to turn each passed value into a number and then do comparisons. If you want an example try: console.log(..."test") in the console you will see each letter placed separately – Patrick Evans Commented Jan 7, 2018 at 18:26
  • 6 I'm really not sure why many of the current answers aren't directly answering the actual question: "What are the rules to use spread syntax to expand a string in a function call", but the spread operator will simply convert each character of the string into an argument, for example: "1 2 3" becomes "1", " ", "2", " ", "3" – Khauri Commented Jan 7, 2018 at 18:42
  • Thanks very much @KhauriMcClai, this what I was looking for. – Moaaz Bhnas Commented Jan 7, 2018 at 18:45
  • @KhauriMcClain isn't that exactly what i answered? – Olian04 Commented Jan 7, 2018 at 18:48
 |  Show 1 more comment

4 Answers 4

Reset to default 9

As we can see below, your example is actually spreading to 5 elements, where 2 of them are space characters. You can also see below that the spread operator on a string seems to be the same as using .split('').

const x = "1 2 3";
console.log([...x]);

console.log(x.split(''));

Math.max on an empty string evaluates on empty string like +" " or Number(" ") therefore 0

const str = "1 2 3";
console.log( Math.max(...str))  // ["1"," ","2"," ","3"] >>> [1,0,2,0,3] >>> 3

It's not wise to spread directly a string with numbers, cause 34 8 9 will max to 9. Always split by your separator num.split(/ +/) (one-or-more spaces) beforehand:

const str = "1 34 9 2";

// Issue:
console.log( Math.max(...str) )  // 9   since [1,0,3,4,0,9,0,2]

// Fix:
console.log( Math.max(...str.split(/ +/)) ) // 34

var x = "123";
console.log(Math.max(...x));

// prints 3

... treats the string as an iterable, equivalent to an array with one character mapped to one element.

Spread syntax will result in 5 elements, where 2 of them are space characters:

const x = "1 2 3";
console.log([...x]);

//=> ["1", "", "2", "", "3"]


In detail:

The result of the spread operator will be ["1", "", "2", "", "3"] containing the space characters.

Acting on this result, Math.max() will try and find the biggest number in the array, by converting non-Number types to Number, similar to running Number("1") to convert a String into a Number. The space characters are converted into a Number 0, similar to Number("") === 0.

So, you're left with the following list of numbers: [ 1, 0, 2, 0, 3 ] upon which Math.max() will pick 3 as the biggest number of all.

发布评论

评论列表(0)

  1. 暂无评论