Hackforge Academy

Category: javascript

what is JSON.stringify is a JavaScript

Published on 06 May 2026

Explanation

JSON.stringify() is a JavaScript method used to convert a JavaScript object, array, or value into a JSON string. It is mainly used when sending data to APIs, storing data in localStorage, or converting objects into transferable string format.

Code:

const user = {
  name: 'Praveen',
  age: 25
};

const jsonData = JSON.stringify(user);

console.log(jsonData);
// Output: {"name":"Praveen","age":25}

Explanation

It is commonly used while sending data in API requests because APIs usually accept JSON string data.

Code:

const data = {
  title: 'React Course',
  price: 999
};

fetch('https://api.example.com/course', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify(data)
});

Explanation

JSON.stringify() is used to store objects in localStorage because localStorage only stores strings.

Code:

const user = {
  name: 'Kumar',
  city: 'Chennai'
};

localStorage.setItem('user',
 JSON.stringify(user));

Explanation

You can convert arrays into JSON strings using JSON.stringify().

Code:

const numbers = [1, 2, 3, 4];

const result = JSON.stringify(numbers);

console.log(result);
// Output: [1,2,3,4]

Explanation

JSON.stringify() can format JSON output using additional parameters for readability. The third parameter adds indentation spaces.

Code:

const student = {
  name: 'Arun',
  marks: 90
};

const formatted = 
JSON.stringify(student, null, 2);

console.log(formatted);
/* Output:
{
  "name": "Arun",
  "marks": 90
}
*/

Explanation

Functions, undefined values, and symbols are not included in JSON.stringify() output because JSON format does not support them.

Code:

const obj = {
  name: 'Test',
  age: undefined,
  greet: function() {
    console.log('Hello');
  }
};

console.log(JSON.stringify(obj));
// Output: {"name":"Test"}

πŸš€ Learn Spring Boot with real-world projects

πŸ’‘ Build REST APIs step by step

🧠 Improve backend development skills

🎯 Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

🐍

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
βš›οΈ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
β˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community