Home >Backend Development >PHP Tutorial >What is the Javascript Equivalent of PHP\'s \'list()\' Function?
Destructing Assignment: The Javascript Equivalent of PHP's 'list()' Function
PHP's 'list()' function is a convenient way to assign values from an array to individual variables. For instance, the following code extracts the first two elements from an array:
<code class="php">$matches = array('12', 'watt'); list($value, $unit) = $matches;</code>
This assigns the string '12' to the variable 'value' and the string 'watt' to the variable 'unit'.
Javascript 1.7 introduced destructuring assignment, which provides similar functionality. This syntax allows you to unpack the elements of an array into individual variables in a single line of code.
The following Javascript code is equivalent to the PHP 'list()' function example:
<code class="javascript">var [value, unit] = ['12', 'watt'];</code>
This code assigns the first element of the array to the variable 'value' and the second element to the variable 'unit'.
Destructuring assignment is supported in all modern browsers except Internet Explorer. It's a convenient and concise way to assign values from an array to individual variables, making your code more readable and maintainable.
The above is the detailed content of What is the Javascript Equivalent of PHP\'s \'list()\' Function?. For more information, please follow other related articles on the PHP Chinese website!