I did not understand what is those in console.log: JSON.stringify? and what is this testArr?

Tell us what’s happening:
Describe your issue in detail here.

  **Your code so far**

function nextInLine(arr, item) {
// Only change code below this line
arr.push(item);
return arr.shift();
// Only change code above this line


}

// Setup
var testArr = [1,2,3,4,5];

// Display code
console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine(testArr, 6));
console.log("After: " + JSON.stringify(testArr));
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.101 Safari/537.36

Challenge: Stand in Line

Link to the challenge:

JSON.stringify converts a JS object, array or primitive (or any combination of these), to a string.

The string is formatted in a format called JSON, which is commonly used to pass data around between computers on networks (eg the internet).

The reason this is done is that if you do this:

var testArr = [1,2,3,4,5];

console.log(testArr);

FCCs console turns the array into a string, but the output looks like:

1,2,3,4,5

Which tends to be confusing for learners.

If you run JSON.stringify, what it does is keep the syntax in place:

[1, 2, 3, 4, 5]

So it’s less confusing for learners

2 Likes

Also, by converting the array to a primitive string value, what is being logged is a snapshot at that moment. If we simply logged the array itself, we are showing a reference to that memory location - and whenever the array itself is updated, the logged value would dynamically update, even on values already logged.

2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.