Tag Archives: Codecademy

The Basics of JavaScript

For the past month or so I have become somewhat immersed in JavaScript. As I already knew the basics of HTML and CSS through using WordPress and other CMS tools, I took the plunge and started to learn JavaScript, the other core web development language, using Codecademy and w3schools.com.

What is JavaScript?

JavaScript is an object-orientated programming language that is derived from C + +. It is a scripting language normally used to create interactive effects within web browsers. JavaScript’s dynamic capabilities include run-time object construction, variable parameter lists, function variables and event loops, some of which I will discuss below.

JavaScript Syntax & Operators

Variables are containers for storing data values. These are declared by the var keyword:

Var

Functions are used to perform defined tasks. These are declared by the function keyword.

2016-08-25 14_30_35-Introduction to Functions in JS _ Codecademy

A function will only be executed when it is called/invoked. e.g. In this case the function is called “greeting” so the input below will return “Great to see you Dave”.

greeting ("Dave")

Return is a statement used to stop the execution of a function. It will also return the value of a function.

var x = myFunction(2, 4);        

function myFunction(a, b) {
    return a * b;                
}

In the example above, x will be returned as ‘8’.

Console.log commands are used to display data . Console.log() will take whatever is inside the parentheses and log it to the console.

2016-08-25 16_27_53-Getting Started with Programming _ Codecademy

In this case, the information logged to the console will appear as:

2016-08-25 16_29_59-Getting Started with Programming _ Codecademy

If/else statements are used to make decisions in the code. An if/else statement will execute one block of code if a specified condition is true, if the condition is false, another block of code can be executed.

if-else

Loops are used to execute a block of code a number of times. Loops are useful if you want to run the same code repeatedly with different values. It’s all about getting the computer to do the legwork for you, as opposed to typing out numerous lines of code.

Here is an example of a for loop. This will run 5 times, with values of ‘step’ 0 to 4.

var step;
for (step = 0; step < 5; step++) {
  console.log('Walking east one step');
}

Different loop types include:

for – this will loop through a block of code a number of times.
while – this will loop through a block of code while a specified condition is true.
for/in – this will loop through the properties of an object.
do/while – this will also loop through a block of code while a specified condition is true.

Arrays are special types of variable that can used to hold multiple values at one time.

For example, if you wanted to list a number of values from one array you could use a block of code like this:

var beers = ["Heineken", "Amstel", "Carling", "Fosters"];
for (var i = 0; i < beers.length; i++) {
console.log (beers [i]) ;
}

This would return:

Heineken
Amstel
Carling
Fosters
Advertisements