Ready. Start with one instruction.
Loading the learning workspace…
FROM YOUR FIRST INSTRUCTION TO A WORKING APPLICATION
Learn JavaScript from scratch through small working examples, visual experiments and complete projects. No account or AI key required.
Predict → Experiment → Build → Test → Explain
Ready. Start with one instruction.
Loading the learning workspace…
push appends a value to an existing array. It mutates that array and returns its new length, not the array itself. To preserve the caller’s list, this helper first uses slice with no arguments to make a shallow copy. It appends to the copy and then returns the copy. Removing the last value uses pop; reading an index does not remove anything. These operations let a shopping list add and remove entries while keeping the ownership of state clear.
function solve(items) {
const copy = items.slice();
copy.push("Build");
return copy;
}
console.log(solve(["Learn","Run"]));["Learn","Run","Build"]
push appends a value to an existing array. It mutates that array and returns its new length, not the array itself. To preserve the caller’s list, this helper first uses slice with no arguments to make a shallow copy. It appends to the copy and then returns the copy. Removing the last value uses pop; reading an index does not remove anything. These operations let a shopping list add and remove entries while keeping the ownership of state clear.
function solve(items) {Declare a function with a parameter receiving the actual argument supplied by each call. The definition does not execute its body yet.
const copy = items.slice();Create a shallow copy of the array so the following mutation does not change the source array.
copy.push("Build");Append this value to the existing copy. push mutates the array and returns its new length; the following return sends the array itself.
return copy;Evaluate this expression, end the function and give the resulting value to its caller. It is not automatically printed.
}Close the current instruction block.
console.log(solve(["Learn","Run"]));Evaluate the argument and display its value at this point. Quoted text is literal; an unquoted identifier reads its current binding.
function solve(items) {
const copy = items.slice();
copy.push("Build");
return copy;
}
console.log(solve([]));["Build"]
push appends a value to an existing array. It mutates that array and returns its new length, not the array itself. To preserve the caller’s list, this helper first uses slice with no arguments to make a shallow copy. It appends to the copy and then returns the copy. Removing the last value uses pop; reading an index does not remove anything. These operations let a shopping list add and remove entries while keeping the ownership of state clear.
function solve(items) {Declare a function with a parameter receiving the actual argument supplied by each call. The definition does not execute its body yet.
const copy = items.slice();Create a shallow copy of the array so the following mutation does not change the source array.
copy.push("Build");Append this value to the existing copy. push mutates the array and returns its new length; the following return sends the array itself.
return copy;Evaluate this expression, end the function and give the resulting value to its caller. It is not automatically printed.
}Close the current instruction block.
console.log(solve([]));Evaluate the argument and display its value at this point. Quoted text is literal; an unquoted identifier reads its current binding.
function solve(items) {
const copy = items.slice();
copy.push("Build");
return copy;
}
console.log(solve(["Code"]));["Code","Build"]
push appends a value to an existing array. It mutates that array and returns its new length, not the array itself. To preserve the caller’s list, this helper first uses slice with no arguments to make a shallow copy. It appends to the copy and then returns the copy. Removing the last value uses pop; reading an index does not remove anything. These operations let a shopping list add and remove entries while keeping the ownership of state clear.
function solve(items) {Declare a function with a parameter receiving the actual argument supplied by each call. The definition does not execute its body yet.
const copy = items.slice();Create a shallow copy of the array so the following mutation does not change the source array.
copy.push("Build");Append this value to the existing copy. push mutates the array and returns its new length; the following return sends the array itself.
return copy;Evaluate this expression, end the function and give the resulting value to its caller. It is not automatically printed.
}Close the current instruction block.
console.log(solve(["Code"]));Evaluate the argument and display its value at this point. Quoted text is literal; an unquoted identifier reads its current binding.
Repair the intentionally changed operation so the example produces the exact expected result. Then try the extension without copying the answer.
function solve(items) {
const copy = items.slice();
copy.pop();
return copy;
}
console.log(solve(["Learn","Run"]));function solve(items) {
const copy = items.slice();
copy.push("Build");
return copy;
}
console.log(solve(["Learn","Run"]));Return the updated copy rather than the numeric length returned by push.
Which operation changed the result, why did it work, and what would you test next?
Use a different value that could expose a boundary or mistake. Predict the result first, run it, and explain how the concept would help in a small real application.
Related: Previous lesson Next lesson
100 lessons across 20 modules, covering variables, operators, strings, conditions, loops, functions, scope, arrays, objects, DOM, events, state, testing, asynchronous JavaScript and application development.