1. Technology

Variables, Functions, Properties and Methods

By

These four terms are all used in referencing various parts of the code when you are writing a program or script. Each of them has a particular meaning and in many programming languages the distinction between them is important. With the way JavaScript works though the distinction between these terms is not as great as it is in many other languages.

The first two terms - variable and function - are normally used to refer to a field where data can be stored and a block of code that can be run, respectively. A variable can be of a number of different types depending on what sort of data we are going to store in it. Some languages are strongly typed which means that you need to define what type of data a variable is allowed to contain when you define the variable. JavaScript is weakly typed which means that it works out what type of data is held in the variable by examining the variable.

One type of data that you can store in a variable in JavaScript is a block of code that can be run. Now as previously mentioned, a block of code that can be run is known as a function. So in JavaScript we can store a function in a variable. The distinction between a function stored in a variable and anything else that we have stored in a variable is that a function can be run whereas other data types cannot. So when we actually come to run the block of code it makes more sense to refer to it as a function rather than a variable as that is the type of variable that we are referencing.

Note that this relationship between variables and functions does not necessarily apply to other languages.

As well as variables and functions that are completely separate and stand alone, many languages including JavaScript also allow us to define objects which can have a number of variables and functions associated with them. When a variable belongs to a particular object we refer to it as a property of that object. Similarly when a function belongs to a particular object we refer to it as a method of that object.

JavaScript in fact does not actually allow you to have variables and functions that are not attached to an object. Depending on which environment you are going to run the JavaScript in there will always be a default object to which any variables and functions that you define separately will be attached. When you are running the script in a browser that default object is the window object. Any global variables and functions you define will be considered to be properties and methods of the window object.

In JavaScript, functions are defined as being a type of object. Any variables that you define locally inside a function will be properties of the object that JavaScript considers the function to be.

So in JavaScript, all variables are also properties and all functions are also methods.So we can use variable and property interchangeable, and function and method interchangeable, although where an object other than the default window object is concerned using the latter terminology makes more sense as in those instances the object they belong to is explicitly defined.

In the same way that functions are a type of variable in JavaScript, we can now also say that methods are simply a type of property that can be run as far as JavaScript is concerned.

In fact once you take into account all of these relationships between what these different terms mean, we can say that in JavaScript we have objects that have properties where some of the properties may also be objects.

So if you are reading about JavaScript and the person who wrote the article appears to have used a different term to refer to the particular data container to what you would have expected them to use, you need to remember that all of these terms are related somewhat differently in JavaScript to the more separate ways in which many other languages treat them.
  1. About.com
  2. Technology
  3. JavaScript
  4. Javascript Tutorials
  5. Variables, Functions, Properties and Methods

©2014 About.com. All rights reserved.