Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

add an element to the front of an input list in javascript

/* List structure: a nested set of objects. NOT an array.
Each object holds reference to its successor in the object chain. */

// Add a value to the front of a list (object chain)
// Use the list itself as input
function prepend(value, list){
  return {value, rest:list};
};

console.log(prepend(10, prepend(20, null)));
// → {value: 10, rest: {value: 20, rest: null}}
Source by eloquentjavascript.net #
 
PREVIOUS NEXT
Tagged: #add #element #front #input #list #javascript
ADD COMMENT
Topic
Name
5+1 =