1. Computing

JavaScript for Beginners

An Introduction to Writing Your Own JavaScript

By

While there are some people who already have experience with other programming languages before they decide to learn JavaScript, there are also a large number of people who have no prior programming experience at all. The closest that a beginner like you may have come to programming might have been to copy and paste existing scripts someone else has already written into your web pages. The step from there to writing your own scripts (or programs) might appear to be a huge one but a lot of the mystery that currently confronts you is mostly because you do not yet understand the terminology that languages such as JavaScript uses. A great many of the things that a beginner like you will need to know to be able to write you own JavaScript are things that you already know but under different names. Once you learn what the various names in JavaScript mean you will be able to apply your existing knowledge to get started with writing your own code using JavaScript.

If you remember back to when you were studying language back at school you will remember that there was a lot of time spent teaching you grammar. Well with JavaScript, instead of calling it grammar we call it syntax and instead of having nouns and verbs we have variables and operators. Once you understand what variables and operators are you will have made a great deal of progress toward being able to write your own JavaScript.

JavaScript Variables for Beginners

To be able to understand what variables are and how they differ from the value that they contain you might consider the difference between a car parking space and the car that just happens to be parked in that spot at the moment. Just as a JavaScript variable can hold different values at different times, a parking space can have different cars parked there at different times. The variable in our code acts as a placeholder for the different values that we may want to have the code process.

There are a number of different types of variables but unlike some other programming languages where you need to specify the type of value a variable is allowed to contain, in JavaScript a variable can contain any type of value and its type is determined by the value it contains. The same variable could hold a number at one time, some text at another time, a date at yet another time and so on. Of course doing that would be confusing since then we would never quite know what to expect. Generally even though a variable can hold different types at different times, we will generally try to control what type of content goes where.

One way that we can keep track of what our variables are supposed to contain is to give them meaningful names. Variables in JavaScript are allowed to contain both lowercase and uppercase letters, numbers, as well as dollar signs and underscores. The convention many people use for naming variables in JavaScript is called camelcase where you have two or more words that describe the content with everything in lowercase except for the first letter of the second and subsequent words. An alternate notation simply separates the words with underscores.

Generally when you first define a variable in JavaScript you precede the variable name with the reserved word var. You can use variables without first defining them but defining your variables when you first reference them gives you better control and may prevent interference between two scripts attached to the same web page.

JavaScript Operators for Beginners

Of course you can’t do much with variables unless you can change their value. Variables are called that because we can operate on them to vary their content from one value to another. Many of these operators are things you are already familiar with (such as many of the numeric operators) but some do not work quite the way you’d expect. As with the arithmetic that you learnt back at school there are ‘sums’ that you can specify where there would be several possible results depending on which order the parts of the calculation are performed in and there is therefore a defined order of precedence as to the order in which the calculations are always performed in so as to allow everyone to get the same answer. A similar order of precedence to what you are used to also applies in JavaScript.

One operator that doesn’t work quite the way you might expect is =. In JavaScript a single = by itself means that the result of the operations to its right is to be assigned to the variable specified to its left. It is therefore known as an assignment operator. It can also be combined with other operators to produce shorter code.

Not everything stored in variables are numbers and one of the numeric operators can also be used with text where it has a different meaning. A simple trap for beginners is to perform an operation such as ‘1’+’1’ and wonder why the answer ends up as ‘11’ rather than 2. The reason for the unexpected answer is because the text operator + means to concatenate the two text fields.

Understanding how variables and operators work is the first step in learning to write your own JavaScript and is the main concept that you need to properly understand before you attempt to write any JavaScript of your own. At this point you might like to take a look at some of the JavaScript that has been written by others to see if you can identify the variables and operators that they have used in their code and perhaps even see if you can work out what some of the statements in their code do.

Related Video
PowerPoint Presentations for Beginners

©2014 About.com. All rights reserved.