Using JavaScript Variables

Script Syntax

JavaScript is a case sensitive programming language. An example keyword in JavaScript is var. Var will not be accepted. myName and MyName are different values.

var A=1;
Var B
=2; //This is an error

Most lines of code start with a keyword or command, for example var, for or function.

var A=1,B=2;
var C=Add(A,B);
return Add1+Add2;

Simple commands with single lines of code, end with a semicolon.

var Message="Hello";

More complex commands require blocks of code. Blocks of code are surrounded by curly brackets. The commands for, while and if, all require blocks of code.


Code is treated as one line. So semicolons are the only way to know when one command ends and another starts. This code has two different commands, and is still valid on just one line.

var A=true;

var A=true;if(A){alert("Correct");}

Text should be surrounded by quotes. In JavaScript, text is called strings.

var Message1="Hello",Message2="World";
alert(Message1+" "+Message2);

Arithmetic and mathematical formula can be calculated.

var Result=(123.2/8)*(0.32+7);

Some values have sub values and sub functions. These can be accessed by adding a point between the value and the sub value or function.


Using JavaScript Variables