Menu
Web Design
  • Home
  • About Us
  • Contact Us
  • Disclaimer
  • Privacy Policy
Web Design

Objects in javaScript

objects are used to represent real-world entities, such as a person or a car, or to organize data into a logical structure. An object is a collection of key-value pairs, where each key is a string (also known as a property name) and the corresponding value can be any valid JavaScript data type, including other objects.

There are several ways to create objects in JavaScript. One common way is to use object literals, which is a shorthand syntax for creating objects:

let person = {

name: “John”,

age: 30,

address: {

street: “123 Main St”,

city: “Anytown”,

state: “CA”

}

};

In this example, person is an object with three properties (name, age, and address). The address property is itself an object with three properties (street, city, and state).

Another way to create objects is to use constructor functions and the new keyword:

function Person(name, age) {

this.name = name;

this.age = age;

}

let john = new Person(“John”, 30);

In this example, Person is a constructor function that defines the name and age properties for a Person object. The new keyword is used to create a new instance of the Person object, with the name and age values set to “John” and 30, respectively.

JavaScript objects are mutable, which means that their properties can be changed or added at any time:

person.age = 31;

person.gender = “Male”;

In this example, the age property of the person object is changed to 31, and a new gender property is added.

JavaScript also has built-in objects, such as Math and Date, that provide a set of methods and properties for performing common operations. For example, the Math object provides methods for performing mathematical operations:

let x = Math.round(4.7); // returns 5

let y = Math.pow(2, 3); // returns 8

In this example, the Math.round method rounds the number 4.7 to the nearest integer (which is 5), and the Math.pow method raises the number 2 to the power of 3 (which is 8).

objects are a fundamental concept in JavaScript and are used extensively in modern web development. Understanding how to create and manipulate objects is essential for building dynamic and interactive web applications. click here to go back.

Web Design

  • Facebook
  • Linkedin
  • Twitter
  • Instagram
  • Whatsapp
  • Contact :
  • Email: contact@webdesign.com
  • Phone: +1-XXX-XXX-XXXX
  • Address: 1234 Example Street, Suite 567, Anytown, USA
  • here you learn
  • HTML
  • CSS
  • javaScript
  • Home
  • About Us
  • Contact Us
  • Disclaimer
  • Privacy Policy
©2023 Web Design | Powered by WordPress and Superb Themes!