function filterAndCheckValue(array, valueToCheck) { const filteredArray = array.filter(item => item === valueToCheck); if (filteredArray.length === 0) { return array; } return filteredArray; } const myArray = [1, 2, 3, 4, 5]; const valueToFind = 6; const result = filterAndCheckValue(myArray, valueToFind); console.log(result); // Output: [1, 2, 3, 4, 5]
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter