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