Javascript Basics

JavaScript is a very powerful coding language that runs on modern web browsers such as Chrome, Firefox and Internet Explorer and many other computers in different ways.

For the basics we will concentrate on web browsers as anyone viewing this on a computer already has everything they would need to go ahead and try out some javascript.

Value Types

In JavaScript variables can be set to different values.  The type of values which can be used are as follows.

  • number
  • string
  • boolean
  • object
  • function
  • undefined
var size = 10; // number
var color = 'yellow'; // string
var inflatable = false; // boolean
var ball = { // array
 type: 'basketball',
 color: 'orange'
};
var setBallColor = function(color){  // function
 ball.color = color;
};
Branches

Branches consist of if statements and switch statements that will send the program execute down one path or another depending on given conditions.

For instance continuing the example from above lets use a type of ball as a way to demonstrate using a branch to set the cost of the ball.

var ball = {
	type: 'basketball'	
};

// example of a branch using if statements to set the cost of a ball
if(ball.type == 'basketball')
	ball.cost = 25.00;
else if(ball.type == 'baseball')
	ball.cost = 7.50;

// the same example using a switch statement
switch(ball.type){
	case 'basketball':
		ball.cost = 25.00;
		break;
	case 'baseball':
		ball.cost = 7.50;
		break;
}