JavaScript Objects

## JS Basic

JS HOME JS Introduction JS How To JS Output JS Statements JS Comments JS Variables JS Data Types JS Objects JS Functions JS Operators JS Comparisons JS Conditions JS Switch JS Loop For JS Loop While JS Breaks JS Errors JS Validation

## JS Advanced

JS Object JS Number JS String JS Date JS Array JS Boolean JS Math JS RegExp

## JS HTML DOM

DOM Intro DOM Methods DOM Document DOM Elements DOM HTML DOM CSS DOM Events DOM Navigation DOM Nodes DOM Nodelist

## JS Browser BOM

JS Window JS Screen JS Location JS History JS Navigator JS PopupAlert JS Timing JS Cookies

## JS Libraries

JS Libraries JS jQuery JS Prototype

## JS Examples

JS Basic Examples JS Objects Examples JS DOM Examples JS HTML Examples JS Browser Examples JS Quiz JS Certificate JS Summary

## JS References

Javascript Objects HTML DOM Objects

# JavaScript Objects

« Previous
Next Chapter »

Almost everything in JavaScript can be an Object: Strings, Functions, Arrays, Dates....

Objects are just data, with properties and methods.

## Properties and Methods

Properties are values associated with objects.

Methods are actions that objects can perform.

## A Real Life Object. A Car:

Object | Properties | Methods
--- | --- | ---
| car.name = Fiat car.model = 500 car.weight = 850kg car.color = white | car.start() car.drive() car.brake()

The properties of the car include name, model, weight, color, etc.

All cars have these properties, but the property values differ from car to car.

The methods of the car could be start(), drive(), brake(), etc.

All cars have these methods, but they are performed at different times.

## Objects in JavaScript:

In JavaScript, objects are data (variables), with properties and methods.

You create a JavaScript String object when you declare a string variable like this:

var txt = new String("Hello World");

String objects have built-in properties and methods:

Object | Property | Method
--- | --- | ---
"Hello World" | txt.length | txt.indexOf("World")

The string object above has a length property of 11, and the indexOf("World") method will return 6.

| In object oriented languages, properties and methods are often called object members.

You will learn more about properties and the methods of the String object in a later chapter of this tutorial.

## Creating JavaScript Objects

Almost "everything" in JavaScript can be objects. Strings, Dates, Arrays, Functions....

You can also create your own objects.

This example creates an object called "person", and adds four properties to it:

## Example

person=new Object();
person.firstname="John";
person.lastname="Doe";
person.age=50;
person.eyecolor="blue";
Try it yourself »

There are many different ways to create new JavaScript objects, and you can also add new properties and methods to already existing objects.

You will learn much more about this in a later chapter of this tutorial.

## Accessing Object Properties

The syntax for accessing the property of an object is:

objectName.propertyName

This example uses the length property of the String object to find the length of a string:

var message="Hello World!";
var x=message.length;

The value of x, after execution of the code above will be:

12

## Accessing Object Methods

You can call a method with the following syntax:

objectName.methodName()

This example uses the toUpperCase() method of the String object, to convert a text to uppercase:

var message="Hello world!";
var x=message.toUpperCase();

The value of x, after execution of the code above will be:

HELLO WORLD!

## Did You Know?

| It is common, in object oriented languages, to use camel-case names. You will often see names like someMethod(), instead of some_method().

« Previous
Next Chapter »

## Your suggestion:

Your E-mail (optional):

Page address:

Description:

Close [X]

## Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

## WEB HOSTING

Best Web Hosting UK Reseller Hosting Wow! $5 SSD Hosting

## WEB BUILDING

Download XML Editor
FREE Website BUILDER FREE Website Creator

## WEB RESOURCES

Web Statistics Web Validation

## SHARE THIS PAGE