JavaScript Tutorials

Overview

JavaScript objects are variables that can contain many values, and are created using the object literal, the new keyword, or using an object constructor.

Objects are named values and are made up of a name for the property and the value for the property.

The following is an object called “car”.

  • type: “Maserati”
  • model: “Quattroporte”
  • color: “white”

Note

A JavaScript object is a “thing”. It is not a copy of that thing. So any changes made to an object, will ultimately change that thing.

For instance, if we have a property/value pair of color: “white”, and we make a change so that the value is “red”, that “thing” (in this case the car object) is now red, not white.

Using the Object Literal

An object literal is a list of name:value pairs inside curly braces {}.

All on one line:

const car = {type: "Maserati", model: "Quattroporte", color: "white"};

Using multiple lines:

const car = {
    type:"Maserati", 
    model:"Quattroporte", 
    color:"white"
};

Creating an empty JavaScript object, and adding the properties afterward:

// create an object
const car = {};

// add properties
car.type = "Maserati";
car.model = "Quattroporte";
car.color = "white";

Using the new Keyword

Creating an empty JavaScript object, and adding the properties afterward:

// create an object
const car = new Object();

// add properties
car.type = "Maserati";
car.model = "Quattroporte";
car.color = "white";

Note

The two methods for creating and adding properties to a JavaScript work the same way. However, the object literal method is preferred for simplicity.


JavaScript Notes:

  • When using JavaScript, single or double quotation marks are acceptable and work identically to one another; choose whichever you prefer, and stay consistent
  • JavaScript is a case-sensitive language; firstName is NOT the same as firstname
  • JavaScript variables are case sensitive (x is not the same as X)
  • Arrays count starting from zero NOT one; so item 1 is position [0], item 2 is position [1], and item 3 is position [2] … and so on
  • JavaScript variables must begin with a letter, $, or _

We’d like to acknowledge that we learned a great deal of our coding from W3Schools and TutorialsPoint, borrowing heavily from their teaching process and excellent code examples. We highly recommend both sites to deepen your experience, and further your coding journey. We’re just hitting the basics here at 1SMARTchicken.