.github
api-server
client
config
curriculum
docs
guide
arabic
chinese
english
3d
accessibility
agile
algorithms
android-development
angular
angularjs
apache
aspnet
bash
blender
blockchain
bootstrap
bsd-os
bulma
c
canvas
certifications
apis-and-microservices
coding-interview-prep
data-visualization
front-end-libraries
information-security-and-quality-assurance
javascript-algorithms-and-data-structures
basic-algorithm-scripting
basic-data-structures
basic-javascript
debugging
es6
functional-programming
add-elements-to-the-end-of-an-array-using-concat-instead-of-push
index.md
apply-functional-programming-to-convert-strings-to-url-slugs
avoid-mutations-and-side-effects-using-functional-programming
combine-an-array-into-a-string-using-the-join-method
combine-two-arrays-using-the-concat-method
implement-map-on-a-prototype
implement-the-filter-method-on-a-prototype
introduction-to-currying-and-partial-application
learn-about-functional-programming
pass-arguments-to-avoid-external-dependence-in-a-function
refactor-global-variables-out-of-functions
remove-elements-from-an-array-using-slice-instead-of-splice
return-a-sorted-array-without-changing-the-original-array
return-part-of-an-array-using-the-slice-method
sort-an-array-alphabetically-using-the-sort-method
split-a-string-into-an-array-using-the-split-method
understand-functional-programming-terminology
understand-the-hazards-of-using-imperative-code
use-the-every-method-to-check-that-every-element-in-an-array-meets-a-criteria
use-the-filter-method-to-extract-data-from-an-array
use-the-map-method-to-extract-data-from-an-array
use-the-reduce-method-to-analyze-data
use-the-some-method-to-check-that-any-elements-in-an-array-meet-a-criteria
index.md
intermediate-algorithm-scripting
javascript-algorithms-and-data-structures-projects
object-oriented-programming
regular-expressions
index.md
responsive-web-design
index.md
chef
clojure
cloud-development
codeigniter
computational-genomics
computer-hardware
computer-science
containers
cplusplus
csharp
css
d3
data-science-tools
design-patterns
designer-tools
developer-ethics
developer-tools
devops
docker
documentation
drupal
electron
elixir
elm
erlang
fsharp
game-development
gatsbyjs
git
go
groovy
haskell
haxe
hibernate
html
ionic
java
javascript
joomla
jquery
julia
kotlin
laravel
linux
logic
machine-learning
mathematics
meta
miscellaneous
mobile-app-development
mongodb
natural-language-processing
neovim
network-engineering
nginx
nodejs
optical-alignment
php
product-design
progressive-web-apps
puppet
python
r
raspberry-pi
react
react-native
redux
rest-api
robotics
rt-os
ruby
rust
sass
security
semantic-ui
software-engineering
sql
ssh
svg
svn
swift
terminal-commandline
tomcat
tools
typescript
typography
user-experience-design
user-experience-research
vagrant
vim
virtualbox
visual-design
voice
vue
vue-cli
web-augmented-reality
web-components
web-performance
web-virtual-reality
wordpress
working-in-tech
xml
portuguese
russian
spanish
mock-guide
tools
.editorconfig
.eslintignore
.eslintrc
.gitattributes
.gitignore
.node-inspectorrc
.prettierrc
.snyk
.travis.yml
.vcmrc
CODE_OF_CONDUCT.md
CONTRIBUTING.md
LICENSE.md
README.french.md
README.italian.md
README.md
docker-compose-shared.yml
docker-compose.yml
lerna.json
netlify.toml
package-lock.json
package.json
sample.env
26 lines
801 B
Markdown
26 lines
801 B
Markdown
![]() |
---
|
||
|
title: Add Elements to the End of an Array Using concat Instead of push
|
||
|
---
|
||
|
|
||
|
## Add Elements to the End of an Array Using concat Instead of push
|
||
|
Where the `push` method adds new element to the end of the orginal array, the `concat` method creates a new array containing the elements from the original array and the new element. The original array remains the same when using `concat`.
|
||
|
|
||
|
#### Relevant Links:
|
||
|
- [Array.prototype.concat()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat)
|
||
|
|
||
|
## Solution
|
||
|
```javascript
|
||
|
function nonMutatingPush(original, newItem) {
|
||
|
|
||
|
// Add your code below this line
|
||
|
|
||
|
return original.concat(newItem);
|
||
|
|
||
|
// Add your code above this line
|
||
|
}
|
||
|
|
||
|
var first = [1, 2, 3];
|
||
|
var second = [4, 5];
|
||
|
nonMutatingPush(first, second);
|
||
|
|
||
|
```
|