"Everything" in JavaScript is an Object: a String, a Number, an Array, a Date....
In JavaScript, an object is data, with properties and methods.
Properties are values associated with objects.
Methods are actions that objects can perform.
Properties:car.name=Fiat car.model=500 car.weight=850kg car.color=white |
|
Methods: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.
In JavaScript, objects are data (variables), with properties and methods.
When you declare a JavaScript variable like this:
You actually create a JavaScript String object. The String object has a built-in property called length. For the string above, length has the value 5. The String object also have several built-in methods.
Properties:txt.length=5 |
"Hello" |
Methods:txt.indexOf()txt.replace() txt.search() |
|
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.
Almost "everything" in JavaScript is an object. Strings, Dates, Arrays, Functions.
You can also create your own objects.
This example creates an object called "person", and adds four properties to it:
There are many different ways to create new JavaScript objects, and you can also add properties and methods to existing objects.
You will learn much more about this in a later chapter of this tutorial.
The syntax for accessing the property of an object is:
This example uses the length property of the String object to find the length of a string:
The value of x, after execution of the code above will be:
You can call a method with the following syntax:
This example uses the toUpperCase() method of the String object, to convert a text to uppercase:
The value of x, after execution of the code above will be:
| It is common in object oriented languages, to use camel-case function names. You will more often see function names like someMethod() instead of some_method(). |
The perfect solution for professionals who need to balance work, family, and career building.
More than 10 000 certificates already issued!
The HTML Certificate documents your knowledge of HTML.
The HTML5 Certificate documents your knowledge of advanced HTML5.
The CSS Certificate documents your knowledge of advanced CSS.
The JavaScript Certificate documents your knowledge of JavaScript and HTML DOM.
The jQuery Certificate documents your knowledge of jQuery.
The XML Certificate documents your knowledge of XML, XML DOM and XSLT.
The ASP Certificate documents your knowledge of ASP, SQL, and ADO.
The PHP Certificate documents your knowledge of PHP and SQL (MySQL).
Your message has been sent to W3Schools.