After cloning JS array using JSON.parse & JSON.stringify, why array's "undefined" value converts to "null"?

Viewed 65
var arr1 = [1, 2, 3, undefined, 4, 5];
var arr2 = JSON.parse(JSON.stringify(arr1));
console.log(arr2);

Output of the above code will be....

[1, 2, 3, null, 4, 5]

Why it converts it's 4th value from "undefined" to "null"?

1 Answers

Because undefined is a Javascript type. There is no undefined in JSON.

According to the spec:

JSON can represent four primitive types (strings, numbers, booleans, and null) and two structured types (objects and arrays).

Related