Choose an Array Operation

Choose an Array Operation

Choose an array operation by its purpose: reading, adding, removing, selecting or copying. A shallow copy protects the outer array from direct edits but does not clone nested records.

Before this topic, review Copy Values and Collect Arguments if its terms are unfamiliar.

What you’ll learn

  • Explain choose an array operation through the school example.
  • Trace the shown operations and distinguish their result from displayed output.
  • Make the practice change and explain its consequence: The copied outer array changes while the original numeric item remains 70.

Work through the school example

This is JavaScript using language features and the host console. Read the initial values, then follow each operation to its displayed result. A declaration names a value, a function call performs an operation, and console.log displays its arguments.

JavaScript · school.js · complete example

const marks = [70, 80];
const copy = marks.slice();
copy[0] = 90;
console.log(marks[0]);
console.log(copy[0]);

Expected output

70
90
Run in JavaScript compiler →

JavaScript Array How-To Topics

  1. 1How to Declare an Empty Array in JavaScript
  2. 2How to Declare an Array With Initial Values in JavaScript
  3. 3How to Check the Number of Items in an Array in JavaScript
  4. 4How to Get the First Item in an Array in JavaScript
  5. 5How to Get the Last Item in an Array in JavaScript
  6. 6How to Access an Item by Its Position in JavaScript
  7. 7How to Update an Item at a Specific Position in JavaScript
  8. 8How to Add an Item to the End of an Array in JavaScript
  9. 9How to Add an Item to the Beginning of an Array in JavaScript
  10. 10How to Insert an Item at a Specific Index in JavaScript
  11. 11How to Remove the Last Item From an Array in JavaScript
  12. 12How to Remove the First Item From an Array in JavaScript
  13. 13How to Remove an Item From a Specific Position in JavaScript
  14. 14How to Remove All Items From an Array in JavaScript
  15. 15How to Copy Items Into a New Array in JavaScript
  16. 16How to Combine Two or More Arrays in JavaScript
  17. 17How to Split an Array Into Smaller Arrays in JavaScript
  18. 18How to Loop Through All Items in an Array in JavaScript
  19. 19How to Transform Each Item in an Array in JavaScript
  20. 20How to Select Specific Items From an Array in JavaScript
  21. 21How to Count Items That Meet a Condition in JavaScript
  22. 22How to Calculate the Sum of Array Values in JavaScript
  23. 23How to Check if a Value Exists in an Array in JavaScript
  24. 24How to Find the Position of a Value in JavaScript
  25. 25How to Find the Last Position of a Value in JavaScript
  26. 26How to Extract a Section of an Array in JavaScript
  27. 27How to Replace Part of an Array in JavaScript
  28. 28How to Reverse the Order of Items in JavaScript
  29. 29How to Sort Items Alphabetically in JavaScript
  30. 30How to Sort Numbers in Ascending Order in JavaScript
  31. 31How to Sort Numbers in Descending Order in JavaScript
  32. 32How to Sort Objects by a Property in JavaScript
  33. 33How to Check if All Items Meet a Condition in JavaScript
  34. 34How to Check if Any Item Meets a Condition in JavaScript
  35. 35How to Find the First Matching Item in JavaScript
  36. 36How to Find the Index of a Matching Item in JavaScript
  37. 37How to Remove Duplicate Items From an Array in JavaScript
  38. 38How to Count Occurrences of a Value in JavaScript
  39. 39How to Flatten a Nested Array in JavaScript
  40. 40How to Convert an Array to a String in JavaScript
  41. 41How to Create an Array From a String in JavaScript
  42. 42How to Create an Array From Multiple Arguments in JavaScript
  43. 43How to Create an Array With Repeated Values in JavaScript
  44. 44How to Create an Array With a Fixed Size in JavaScript
  45. 45How to Fill an Array With a Single Value in JavaScript
  46. 46How to Check if a Variable Is an Array in JavaScript
  47. 47How to Compare Two Arrays for Equality in JavaScript
  48. 48How to Find the Difference Between Two Arrays in JavaScript
  49. 49How to Find Common Items in Two Arrays in JavaScript
  50. 50How to Shuffle the Items in an Array in JavaScript
  51. 51How to Convert an Array to a Set in JavaScript
  52. 52How to Convert a Set to an Array in JavaScript
  53. 53How to Create a Range of Numbers as an Array in JavaScript
  54. 54How to Remove Falsy Values From an Array in JavaScript
  55. 55How to Check if an Array Is Empty in JavaScript
  56. 56How to Get the Maximum Value in a Numeric Array in JavaScript
  57. 57How to Get the Minimum Value in a Numeric Array in JavaScript
  58. 58How to Clone an Array Without Reference in JavaScript

Recognize the important boundary

Reference how-tos remain available separately from course progress. Check whether an operation mutates the array before choosing it.

Try a small variation

JavaScript · original source for practice

const marks = [70, 80];
const copy = marks.slice();
copy[0] = 90;
console.log(marks[0]);
console.log(copy[0]);

Your task: Replace copy[0] = 90 with copy[0] = 95. Predict the result before running the edited program.

The source is repeated here. If the edit targets the HTML rather than the script, use the markup repeated below; the worked answer also repeats the changed markup.

Worked answer and reasoning

The copied outer array changes while the original numeric item remains 70.

JavaScript · complete worked variation

const marks = [70, 80];
const copy = marks.slice();
copy[0] = 95;
console.log(marks[0]);
console.log(copy[0]);

Expected output

70
95
Run in JavaScript compiler →

Predict the original program

JavaScript · original program for this question

const marks = [70, 80];
const copy = marks.slice();
copy[0] = 90;
console.log(marks[0]);
console.log(copy[0]);
What does the original JavaScript program display, in order?
Choose one answer. You can try again.

Answer and explanation

70 → 90

Choose an array operation by its purpose: reading, adding, removing, selecting or copying. A shallow copy protects the outer array from direct edits but does not clone nested records. Reference how-tos remain available separately from course progress. Check whether an operation mutates the array before choosing it.

Keep the useful idea

Choose an array operation by its purpose: reading, adding, removing, selecting or copying. A shallow copy protects the outer array from direct edits but does not clone nested records.

Check your variation against this explanation: The copied outer array changes while the original numeric item remains 70.

Use the JavaScript array reference library for individual array operations and boundary tests to check a rule independently.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Understand Prototype Lookup →