40 Minutes Learn JavaScript (Part 1)

40 Minutes Learn JavaScript (Part 1)

If you want to learn Object Oriented JavaScript, the first step is you should learn more about Object in JavaScript.


Everything is An Object


In JavaScript almost everything is an object. Even primitive datatypes (except null and undefined) can be treated as objects.

  • Boolean can be objects or primitive data treated as objects

  • Number can be objects or primitive data treated as objects

  • String are also objects or primitive data treated as objects

  • Date are always objects

  • Math and Regular Expression are always objects

  • Array are always objects

  • Even functions are always objects


Objects and Properties


A JavaScript object has properties associated with it. A property of an object can be explained as a variable that is attached to the object. Object properties are basically the same as ordinary JavaScript variables, except for the attachment to objects. The properties of an object define the characteristics of the object. You access the properties of an object with a simple dot-notation :


Like all JavaScript variables, both the object name (which could be a normal variable) and property name are case sensitive. You can define a property by assigning it a value. For example, let’s create an object named myCar and give it properties named make, model, and year. Example :

[code language="JavaScript"]
 var myCar = new Object();
 myCar.make = "Ford";
 myCar.model = "Mustang";
 myCar.yearbuy = 1969;
 myCar.yearBuy = 1990;


Properties Bracket Notation


Properties of JavaScript objects can also be accessed or set using a square bracket notation. Objects are sometimes called associative arrays, since each property is associated with a string value that can be used to access it. So, for example, you could access the properties of the myCar object as following example :

[code language="JavaScript"]
 myCar["make"] = "Ford";
 myCar["model"] = "Mustang";
 myCar["year"] = 1969;

Did you feel something interesting with this code ?

myCar["make"] = "Ford";

An object property name can be any valid JavaScript string, or anything that can be converted to a string, including the empty string. However, any property name that is not a valid JavaScript identifier (for example, a property name that has a space or a hyphen, or that starts with a number) can only be accessed using the square bracket notation. This notation is also very useful when property names are to be dynamically determined (when the property name is not determined until runtime). Example :

[code language="JavaScript"]
 var myObj = new Object(),
 str = "myString",
 rand = Math.random(),
 obj = new Object();
 myObj.type = "Dot syntax"; myObj["
 date created"] = "String with space";
 myObj[str] = "String value";
 myObj[rand] = "Random Number";
 myObj[obj] = "Object";
 myObj[""] = "Even an empty string";


Properties as Variable


You can also access properties by using a string value that is stored in a variable :

[code language="JavaScript"]
 var propertyName = "make";
 myCar[propertyName] = "Ford";
 propertyName = "model";
 myCar[propertyName] = "Mustang";


Deleting Properties


You can remove a non-inherited property by using the delete operator. The following code shows how to remove a property :

[code language="JavaScript"]
 var myobj = new Object();
 myobj.a = 5;
 myobj.b = 12; delete myobj.a; console.log ("a" in myobj)

That’s for now, see you on 40 Minutes Learn JavaScript (Part 2). 🙂



Written by Agung Yuliaji – Project Manager at 41studio

Scroll to Top
%d bloggers like this: