Getting started

Go on youtube and search for “javascript for beginners” or something similar, watch a couple of tutorials then read the W3 schools tutorials too.

Javascript didn’t really get fun to learn until I learnt the fundamentals and then started learning about JQuery – which does some really cool stuff, JavaScript can be a bit more hard going!

Here are some basics just in case you’re lazier than me though:

Put all your javascript inside tags:

alert(“My First JavaScript”);

“print” text onto a site with by using –  document.write

e.g.

document.write(“<p>This is a paragraph.</p>”);

You can place Javscript in the <head> of a website, or in the <body>, or in external javascript files (saved with the extension .js).
The code for using an external Javascript file might look like this:
http://Script1.js

You can group code into blocks using curly brackets {  }
You should end each statement with a semi colon ;
Remember Javascript is case sensitive
You can place comments in your code by preceding the text with // or lace multiple line comments starting with /* and ending with */.


You can use Javascript to alter elements of your website

For example, to alter a paragraph, first you would need to give the paragraph an ID:
<p id=”example”>My First Paragraph</p>

Then you can add some Javascript code to manipulate the identified element of your site. Or just write something in that part of your site like in this example:

document.write(“

My First website with Javascript

“);

Javascript Variables
Explained really well in this tutorial

Variables are used to ‘store’ data. Values are assigned to variables, then you can refer to the variable name rather than the whole value.
A bit like algebra in school:
x=5
y=5
z=x+y

Javascript Functions
A function is code that gets executed when someone (usually a website visitor) calls it – by clicking a mouse button, moving their mouse cursor over a certain part of the web-page, by releasing the mouse button, etc.

A function is written as a code block; inside curly { } braces, preceded by the function name:

function functionname()
{
some code to be executed
}

Javascript If…Else Statements
These statements, allow functions to be executed if certain conditions are true.  For example, if it is past 12pm, I website might greet a visitor with “good afternoon”

if (time>12)
{
x=”Good day”;
}
else
{
x=”Good morning”;
}

JavaScript Loops

Loops often work with Arrays (data sets number from 0).
They are handy for running through all the data once, or more than once – depending on a given “if” or “else” circumstance.

Look at this lovely For Loop example form W3 schools

for (var i=0; i<5; i++)
{
x=x + “The number is ” + i + “<br>”;
}

Try it yourself »

From the example above, you can read:

Statement 1 (var i=0)sets a variable before the loop starts (var i=0).

Statement 2 ( i<5) defines the condition for the loop to run (i must be less than 5).

Statement 3 (i++) increases a value (i++) each time the code block in the loop has been executed.

Other types of loops include –

The While Loop

The while loop loops through a block of code as long as a specified ‘condition’ is true. For example, i being less than 10 (i<10)

The Do/While Loop

“Do the loop, while a certain condition is true”.

Leave a comment