Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

how to compare arrays in js

// THE PROBLEM:
const firstArray = ["cookies", "milk", "chocolate"]
const secondArray = ["cookies", "milk", "chocolate"]
console.log(firstArray == secondArray) // always returns FALSE

// THE SOLUTION:
if(JSON.stringify(firstArray) === JSON.stringify(secondArray)){
  console.log("firstArray is the same as the secondArray")
} else {
  console.log("firstArray is different from the secondArray")
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #compare #arrays #js
ADD COMMENT
Topic
Name
4+1 =