JavaScript .valueOf()
Published Jun 21, 2021Updated Jun 12, 2023
Contribute to Docs
The .valueOf() method returns the value of all the elements of the original array.
Syntax
array.valueOf();
Examples
Create a new array with the same elements as programmingLanguages:
const programmingLanguages = ['JS', 'Python', 'Java', 'C++'];const newArray = programmingLanguages.valueOf();console.log(newArray);// Output: ['JS', 'Python', 'Java', 'C++']
Note: This method will not change the original array. However, if the original array or the variable which holds it (using the
.valueOf()method) is manipulated, the value of the array will change accordingly.
Codebyte Example
The example below uses the .valueOf() method to return the fruits array and assign it to a variable myBag. The code then makes changes to the original fruits array by pushing a new element Pineapple. And the value of myBag will also change because it holds a reference to the same fruits array.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn JavaScript on Codecademy
- Front-end engineers work closely with designers to make websites beautiful, functional, and fast.
- Includes 34 Courses
- With Professional Certification
- Beginner Friendly.115 hours
- Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
- Beginner Friendly.15 hours