Movatterモバイル変換


[0]ホーム

URL:


Menu
×
See More 
Sign In
+1 Get Certified Upgrade Teachers Spaces Bootcamps Get Certified Upgrade Teachers Spaces Bootcamps
   ❮     
     ❯   

Basic JavaScript

JS TutorialJS SyntaxJS VariablesJS OperatorsJS If ConditionsJS LoopsJS StringsJS NumbersJS FunctionsJS ObjectsJS ScopeJS DatesJS Temporal DatesJS ArraysJS SetsJS MapsJS IterationsJS MathJS RegExpJS DestructuringJS Data TypesJS ErrorsJS DebuggingJS ConventionsJS ReferencesJS 2026JS Versions

JS HTML

JS HTML DOMJS EventsJS ProjectsNew

JS Advanced

JS FunctionsJS ObjectsJS ClassesJS AsynchronousJS ModulesJS Meta & ProxyJS Typed ArraysJS DOM NavigationJS WindowsJS Web APIsJS AJAXJS JSONJS jQueryJS GraphicsJS ExamplesJS Reference


JavaScript Objects

What are JavaScript Objects?

Objects arevariables that can store bothvalues andfunctions.

Values are stored askey:value pairs calledproperties.

Functions are stored askey:function() pairs calledmethods.

Real Life Comparison

In real life, we can describe subjects as objects (a car, a person, a house)

Car Object
Car PropertiesCar Methods
car.name = Fiat

car.model = 500

car.weight = 850kg

car.color = white
car.start()

car.drive()

car.brake()

car.stop()

Different cars have the sameproperties, but theproperty values can differ from car to car.

Different cars have the samemethods, but the methods can be performedat different times.


Object Example

This code example assignsmany values (Fiat, 500, white) to anobject named car:

Example

const car = {
  type: "Fiat",
  model: "500",
  color: "white"
};
Try it Yourself »

Note

type,model,andcolor areproperties

"Fiat",500,and"white" areproperty values


Object Literal

Anobject literal "literally"describes an object using a concise syntax with zero or morekey:value pairs insidecurly braces to describe all theobject properties:

{firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"}

How to Create a JavaScript Object

Anobject literal is the simplest and most common way to define a JavaScript object.

All the examples below, create the same JavaScript object:

Example 1

// Create an Object
const person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
Try it Yourself »

Spaces and line breaks are not important. An object literal can span multiple lines:

Example 2

// Create an Object
const person = {
  firstName: "John",
  lastName: "Doe",
  age: 50,
  eyeColor: "blue"
};
Try it Yourself »

You can also create anempty object, and add the properties later:

Example 3

// Create an Object
const person = {};

// Add Properties
person.firstName = "John";
person.lastName = "Doe";
person.age = 50;
person.eyeColor = "blue";
Try it Yourself »

Note

You should declare objects with theconst keyword.

Using the new Keyword

Example 4

Create a new JavaScript object usingnew Object():

// Create an Object
const person = new Object({
  firstName: "John",
  lastName: "Doe",
  age: 50,
  eyeColor: "blue"
});
Try it Yourself »

Note:

There is no need to usenew Object().

For readability, simplicity and speed, use anobject literal instead.



Object Properties

You can access object properties in two ways:

  • Dot notation
  • Bracket notation

Dot Notation

objectName.propertyName
person.firstName;
Try it Yourself »

Bracket Notation

objectName["propertyName"]
person["firstName"];
Try it Yourself »

JavaScript Object Methods

Objects can also havemethods.

Object methods areactions that can be performed on objects.

Object methods arefunction definitions stored asproperty values:

Example

const person = {
  firstName: "John",
  lastName : "Doe",
  age      : 50,
  fullName : function() {
    return this.firstName + " " + this.lastName;
  }
};
Try it Yourself »
PropertyProperty Value
firstNameJohn
lastNameDoe
age50
fullNamefunction() {return this.firstName + " " + this.lastName;}

Note

In an object method,this refers to the object.

In the example above,this refers to theperson object:



Summary

  • Objects are containers forProperties andMethods
  • Properties are namedValues stored askey:value pairs
  • Methods areFunctions stored askey:function() pairs.

In JavaScript, Objects are King

If you Understand Objects, you Understand JavaScript.

In JavaScript, almost "everything" is an object:

  • Objects are objects
  • Maths are objects
  • Dates are objects
  • Arrays are objects
  • Maps are objects
  • Sets are objects
  • RegExp are Objects
  • Errors are Objects

All JavaScript values, exceptprimitives, are objects.


JavaScript Primitives

Aprimitive data type is data type that can only store a single primitive value.

JavaScript defines 7 types of primitive data types:

TypeExample value
string"Hello"
number3.14
booleantrue
bigint12345678901234
nullnull
undefinedundefined
symbolsymbol



×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted ourterms of use,cookies andprivacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved.W3Schools is Powered by W3.CSS.

-->
[8]ページ先頭

©2009-2026 Movatter.jp