Home >Backend Development >PHP Tutorial >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!