How can JS set values ​​for variables in a chain, like

var date = date[0] = the_creation_of_information.split('., '); 

As far as I know, this is possible in PHP, but in JS?

  • You could not check? - Sergey Gornostaev
  • And what result is expected from this? - Grundy
  • Pull from "1887-1888, Italy" date. - Timur Musharapov
  • which one? 1887 or 1888 or both? - Grundy
  • Both, "1887-1888." - Timur Musharapov

3 answers 3

In ES2015 there is not even needed in the indexer. You can use destructuring assignment

 var [date] = "1887-1888 гг., Италия".split('., '); console.log(date); 

    The '=' operator in JS works from right to left.

    That is, your entry is equivalent to the following:

     var date date[0] = the_creation_of_information.split('., '); date = date[0] 

    which will cause an error, since the second line has date = undefined, and therefore date has no date [0] property.

    • That is, in 1 such an operation it is impossible to do this? - Timur Musharapov

    You cannot assign an array to a date variable and its null element at the same time. But you don’t have to assign an array anywhere!

    The index operator ( [] ) works with any expressions, not just variables.

     var date = the_creation_of_information.split('., ')[0];