JavaScript

JavaScript is a high-level, interpreted programming language that is widely used for web development. Initially designed as a client-side scripting language, it runs directly in web browsers, enabling dynamic and interactive user experiences. JavaScript can now be used for server-side development as well.

JSON Array Literals in JavaScript

15 April 2025 | Category:

A JSON array literal is a way to represent a list of values in JavaScript Object Notation (JSON) format.

✅ A JSON array is an ordered collection of values such as strings, numbers, booleans, objects, arrays, or null.


📘 Example: JSON Array Literal

[
  "HTML",
  "CSS",
  "JavaScript"
]

This array contains 3 strings. JSON arrays always use square brackets [ ] and values are separated by commas.


✅ JSON Array of Objects

You can also store objects inside a JSON array:

[
  { "name": "Alice", "age": 25 },
  { "name": "Bob", "age": 30 },
  { "name": "Charlie", "age": 22 }
]

💡 Use Case: Representing a Collection of Items

JSON arrays are perfect for storing:

  • Lists of users
  • Collections of products
  • Multiple responses from a database
  • Grouped data in APIs

🧠 JSON Array vs JavaScript Array

FeatureJSON ArrayJavaScript Array
FormatText-based (for storage)Code-based (for logic)
Quotes for stringsMust use double quotesCan use single/double quotes
Comments❌ Not allowed✅ Allowed
Functions❌ Not allowed✅ Allowed

🛠️ Working with JSON Arrays in JavaScript

🔸 JSON String to JS Array

const jsonString = '["React", "Vue", "Angular"]';

const frameworks = JSON.parse(jsonString);

console.log(frameworks[0]); // Output: React

🔸 JS Array to JSON String

const colors = ["red", "green", "blue"];

const jsonColors = JSON.stringify(colors);

console.log(jsonColors);
// Output: ["red","green","blue"]

✅ Data Types Allowed in JSON Arrays

Each item in a JSON array can be:

  • A string → "JavaScript"
  • A number → 2025
  • A boolean → true
  • Null → null
  • An object → { "name": "Alice" }
  • Another array → [1, 2, 3]

Example (Mixed types):

[
  "Devin",
  99,
  true,
  null,
  { "course": "Web Dev" },
  [1, 2, 3]
]

⚠️ JSON Array Rules

  • Must begin and end with [ and ]
  • Values are comma-separated
  • Strings must use double quotes
  • No functions or comments allowed

✅ Summary

Key PointDescription
StructureOrdered list of values
UseCommonly used in APIs and data storage
CompatibilityEasy to convert to/from JavaScript arrays
Supported typesString, number, boolean, null, object, array