Javascript Objects

Objects

Definition: Objects in Javascript are used to group related data and functions together. Each piece of data inside an object is called a property.

Syntax:

var myComputer = { // variable is assigned to object starts with a curly brace
operatingSystem: 'Mac', // each property starts with a property name, semicolon, property value in quotes if string. Each property is separated by a comma

screenSize: '15 inches',
purchaseYear: '2011'
}; // ends with a curly brace. The last object ends with a semicolon

Calling an Object

To call an entire object, type object name.
To call a property, type the object name followed by a period and property name.

Method

A method is a property of an object that is a function.

For Loop

Example in English: i = 0 // Initialization Say “hey” if i < 3 // Condition Increase i by 1 // Final Expression Syntax: for (initializtion; condition; final-expression) { } Example: for (var i = 0; i < 3; i++) { console.log("hey"); }