Home  >  Article  >  Backend Development  >  Is there a Javascript Equivalent of PHP\'s list() function?

Is there a Javascript Equivalent of PHP\'s list() function?

Patricia Arquette
Patricia ArquetteOriginal
2024-10-19 06:37:01906browse

Is there a Javascript Equivalent of PHP's list() function?

Javascript Equivalent of PHP's list() function

PHP's list() function allows for the assignment of multiple variables from an array. For example:

<code class="php">$matches = array('12', 'watt');
list($value, $unit) = $matches;</code>

This assigns the first element of the $matches array to the $value variable, and the second element to the $unit variable.

Is there a similar function in Javascript?

Answer:

Yes, there is a Javascript equivalent of the list() function in newer versions of Javascript called destructuring assignment. This feature is supported in Mozilla-based browsers and Rhino.

To use destructuring assignment, you use the following syntax:

<code class="javascript">[a, b] = [b, a];</code>

This will swap the values of the a and b variables.

Note:

Destructuring assignment is not supported in Internet Explorer. However, it is supported in all other modern browsers.

The above is the detailed content of Is there a Javascript Equivalent of PHP\'s list() function?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn