Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

JSON.stringify()

A common use of JSON is to send data to a web server.

When sending data to a web server, it must be in string format.

For example, consider the following object in JavaScript:

var obj = {name:“John”, age:30, city:“New York”};

Use the JavaScript function JSON.stringify() to convert the object into a string.

var myJSON = JSON.stringify(obj);

myJSON is now a string and can be sent to the server.

Example

var obj = {name:“John”, age:30, city:“New York”};
var myJSON = JSON.stringify(obj);
document.getElementById(“demo”).innerHTML = myJSON;

Date.now()

Date.now() returns the number of milliseconds elapsed since the “epoch” (January 1, 1970, 00:00:00 UTC).

Example

var timInMSs = Date.now();

Date.now() returns the same value as calling getTime() on a Date object.

Date toISOString()

The toISOString() method converts a Date object into a string, formatted according to the ISO standard.

Example

const d = new Date();
document.getElementById(“demo”).innerHTML = d.toISOString();

Date toJSON()

The toJSON() method converts a Date object into a string formatted as a JSON date, which follows the ISO-8601 standard: YYYY-MM-DDTHH:mm:ss.sssZ.

Example

d = new Date();
document.getElementById(“demo”).innerHTML = d.toJSON();

Property Getters and Setters

ES5 allows you to define object methods using a syntax that resembles property access.

In this example, a getter is created for a property called fullName:

Example

// Create an object:
var person = {
  firstName: “John”,
  lastName : “Doe”,
  get fullName() {
    return this.firstName + ” “ + this.lastName;
  }
};

// Display data from the object using a getter:
document.getElementById(“demo”).innerHTML = person.fullName;

This example defines both a setter and a getter for the language property:

Example

var person = {
  firstName: “John”,
  lastName : “Doe”,
  language : “NO”,
  get lang() {
    return this.language;
  },
  set lang(value) {
    this.language = value;
  }
};

// Set an object property using a setter:
person.lang = “en”;

// Display data from the object using a getter:
document.getElementById(“demo”).innerHTML = person.lang;

This example uses a setter to ensure that updates to the language property are always in uppercase.

Example

var person = {
  firstName: “John”,
  lastName : “Doe”,
  language : “NO”,
  set lang(value) {
    this.language = value.toUpperCase();
  }
};

// Set an object property using a setter:
person.lang = “en”;

// Display data from the object:
document.getElementById(“demo”).innerHTML = person.language;

Object.defineProperty()

Object.defineProperty() is a new method introduced in ES5 that allows you to define an object property and/or modify its value and metadata.

Example

// Create an Object:
const person = {
  firstName: “John”,
  lastName : “Doe”,
  language : “NO”,
};

// Change a Property:
Object.defineProperty(person, “language”, {
  value: “EN”,
  writable : true,
  enumerable : true,
  configurable : true
});

// Enumerate Properties
let txt = “”;
for (let x in person) {
  txt += person[x] + “<br>”;
}

// Display Properties
document.getElementById(“demo”).innerHTML = txt;

The following example is the same code, but it hides the language property from enumeration.

Example

// Create an Object:
const person = {
  firstName: “John”,
  lastName : “Doe”,
  language : “NO”,
};

// Change a Property:
Object.defineProperty(person, “language”, {
  value: “EN”,
  writable : true,
  enumerable : false,
  configurable : true
});

// Enumerate Properties
let txt = “”;
for (let x in person) {
  txt += person[x] + “<br>”;
}
document.getElementById(“demo”).innerHTML = txt;

This example defines a setter and a getter to ensure that updates to the language property are always in uppercase.

Example

// Create an Object:
const person = {
  firstName: “John”,
  lastName : “Doe”,
  language : “NO”
};

// Change a Property:
Object.defineProperty(person, “language”, {
  get : function() { return language },
  set : function(value) { language = value.toUpperCase()}
});

// Change Language
person.language = “en”;

// Display Language
document.getElementById(“demo”).innerHTML = person.language;