You are on page 1of 7

Dark code

  HTML CSS JAVASCRIPT    


 Menu 

JavaScript Object Methods


❮ Previous Next ❯

Example

const person = {

  firstName: "John",

  lastName: "Doe",

  id: 5566,

  fullName: function() {

    return this.firstName + " " + this.lastName;

  }

};

Try it Yourself »

What is this?
In JavaScript, the this keyword refers to an object.

Which object depends on how this is being invoked (used or called).

The this keyword refers to different objects depending on how it is used:

In an object method, this refers to the object.

Alone, this refers to the global object.


Dark mode

D k d
In a function, this refers to the global object.
Dark code
  HTML CSS JAVASCRIPT   
In a function, in strict mode, this is undefined .

In an event, this refers to the element that received the event.

Methods like call() , apply() ,


and bind() can refer this to any object.

Note
this is not a variable. It is a keyword. You cannot change the value of this .

See Also:
The JavaScript this Tutorial

JavaScript Methods
JavaScript methods are actions that can be performed on objects.

A JavaScript method is a property containing a function definition.

Property Value

firstName John

lastName Doe

age 50

eyeColor blue

fullName function() {return this.firstName + " " + this.lastName;}

Methods are functions stored as object properties.

Accessing Object Methods


Dark mode

D k d
You access an object method with the following syntax:
Dark code
  HTML CSS JAVASCRIPT   
objectName.methodName()

You will typically describe fullName() as a method of the person object, and fullName as a
property.

The fullName property will execute (as a function) when it is invoked with ().

This example accesses the fullName() method of a person object:

Example

name = person.fullName();

Try it Yourself »

If you access the fullName property, without (), it will return the function definition:

Example
name = person.fullName;

Try it Yourself »

Adding a Method to an Object


Adding a new method to an object is easy:

Example

person.name = function () {

  return this.firstName + " " + this.lastName;

};

Dark mode

D k d
Dark code
 Try
it Yourself
HTML » CSS JAVASCRIPT   

Using Built-In Methods


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

let message = "Hello world!";

let x = message.toUpperCase();

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

HELLO WORLD!

Example

person.name = function () {

  return (this.firstName + " " + this.lastName).toUpperCase();

};

Try it Yourself »

❮ Previous Next ❯
 

NEW

We just launched

W3Schools videos

Dark mode

D k d
Dark code
  HTML CSS JAVASCRIPT   

Explore now

COLOR PICKER




Get certified

by completing

a JavaScript

course today!

school
w3 s
2
CE

02

TI 2
R

FI .
ED

Get started

CODE GAME

Dark mode

D k d
Dark code
  HTML CSS JAVASCRIPT   

Play Game

Report Error

Spaces

Pro

Get Certified

Top Tutorials
HTML Tutorial

CSS Tutorial

JavaScript Tutorial

How To Tutorial

SQL Tutorial

Python Tutorial

W3.CSS Tutorial

Bootstrap Tutorial

PHP Tutorial

Java Tutorial

C++ Tutorial

jQuery Tutorial

Top References
HTML Reference

CSS Reference

JavaScript Reference

SQL Reference

Python Reference

W3.CSS Reference

Dark mode

Bootstrap Reference

D k d
PHP Reference
Dark code
  HTML CSS JAVASCRIPTHTML Colors   
Java Reference

Angular Reference

jQuery Reference

Top Examples
HTML Examples

CSS Examples

JavaScript Examples

How To Examples

SQL Examples

Python Examples

W3.CSS Examples

Bootstrap Examples

PHP Examples

Java Examples

XML Examples

jQuery Examples

Get Certified
HTML Certificate

CSS Certificate

JavaScript Certificate

Front End Certificate

SQL Certificate

Python Certificate

PHP Certificate

jQuery Certificate

Java Certificate

C++ Certificate

C# Certificate

XML Certificate

FORUM |
ABOUT

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 our terms of use, cookie and privacy policy.

Copyright 1999-2022 by Refsnes Data. All Rights Reserved.

W3Schools is Powered by W3.CSS.

You might also like