I am going to clarify some things I said earlier about objects because it is such an important concept in JavaScript. Objects are created from functions inside of JavaScript. Technically, when you create a string variable you are writing var example = new String(“Welcome”); An this new String() is a function that creates the string object.

When objects are called, they are usually followed by a . that tells us a property or a method is about to be called. Let’s hope this analogy works. Think of yourself as an object called you.


Now, you have properties about yourself. So, if I typed you.hairColor , I would expect the results to be whatever hair color you had. That is an object property in a nutshell.




The variable example is a string and one of its main properties is length. We get 7 because “Welcome” has 7 characters.


Methods are actions of or on the object. Again using you as an object, you.standUp() would make you stand up.




While the strike() method seems vicious, it simply just puts a line through the object string. Notice how the methods have () after them. Sometimes, they require arguments very much similar to functions. Our last example didn’t require one, but now I’ll show you what it looks like if they do.




The method charAt has an argument of 3, which is why it is shown above as charAt(3). The charAt goes through a string and finds which character is at a position in the string. Since, we used 3 as the argument, it returns “c” because c is the 4th character from the left. Remember that most methods start from 0. So, charAt(0) would return “W”.

Referencing JavaScript Objects

As I said earlier, JavaScript supports Object Oriented Programming. It follows a concept of Inheritance, which is a whole complicated idea in itself. Inheritance summarized, simply means that anything that is created by an object takes on its properties and methods. So if you create a string, it will inherit of the properties already preset by the string object.

JavaScript can reference in object in a few ways, but we will only discuss the main ones.



Changed Text

In the above example, we invoke the “document.getElementById()” method. Here, the method takes one argument of “example”, which grabs the div object with the id of “example”. Now we use the property “innerHTML” that references the contents between the “<div id=”example”>” tag and the “</div>”. In the case we are setting the property’s value equal to “Changed Text” (Quick note: Strings in JavaScript are in quotes). Also, the reason “example” is in quotes is because, if it wasn’t in quotes, it would be referencing a variable.

As you can see reference an object’s id is very common, but sometimes it is better to reference an object’s name.


If you enjoyed this resource, support me by sharing this page with others.
Share on Facebook
Share on StumbleUpon
Digg this
Tweet about this on Twitter
Share on Reddit

Leave a Comment

  1. Learning a new computer language is hard enough without the explanations belonging to things that are not even in the code that is being referenced. There have been a number of spots where you reference things that are not in the code… such as in ‘Javascript Objects’ where you show some code and then explain the code using ‘In the above example, we invoke the "document.getElementById()" method. Here, the method takes one argument of "test", which grabs the div object with the id of "test"’. The problem is that "test" appears no where in the example code. I am thinking that you are probably referring to the keyword "example" as opposed to "test", but following this is extremely difficult, if not impossible, if you can’t be more accurate in your explanations.

  2. Referencing the last example, the document.getElementsByName method returns an array of objects. The syntax should be:
    document.getElementsByName(“example2”)[0].innerHTML = “Changed Text”;

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.