-
Comments: One-line comments are delineated by two slashes.
//The entire rest of this line is a comment.
Multiline comments are set off with /*...*/
/*Anything between the slash-stars is a comment.*/
-
Semicolons: Every command line should end with a semicolon. However, most browsers are good at recovering from most forgotten semicolons.
-
Declaring a variable:
var x;
-
Declaring and initializing a variable:
var x=2;
-
Declaring an array:
var a=[];
-
Declaring and initializing an array: Notice that an array can contain different types of objects. This one contains a number, a string, a boolean, and a variable.
var a=[1,'a', true,x];
-
Defining a function: The code below will define a new function
average(x,y)
which will return the average of the variables x
and y
. Note that functions do not have to return values.
function average(x,y){
var r;
r=(x+y)/2;
return(r);
}
Here is a shorter version of the same function:
function average(x,y){
return((x+y)/2);
}
-
For-loops: Here is a basic for-loop that loops through the numbers 0,1, 2,..., 9 and places them in an array.
var i;
var a=[];
for (i=0;i<10;i++){
a[i]=i;
}
Here, i++
is short for i=i+1
. The braces here are optional because there is only one line of code in the loop. Also, it is probably better to declare the looping variable within the loop to limit its scope. Here is a more concise version:
var a=[];
for (var i=0;i<10;i++)
a[i]=i;
We could also use push
to add elements to the end of the array (see below).
-
Conditionals: The code below increases
x
if it is positive and decreases it otherwise. The braces here are optional since there is only one line of code in each block of the conditional.
if (x>0){
x=x+1;
}
else{
x=x-1;
}
-
Push/pop: Add 27 as an element to the end of an array
a
.a.push(27)
Remove the last element from an array a
and place it in a variable x
. x=a.pop()
-
Shift/unshift: Add 27 an element to the beginning of an array
a
.a.unshift(27)
Remove the first element from an array a
and place it in a variable x
. x=a.shift()
-
Alert with popup message:
alert('Hello there!');
-
Prompt the user for input: Note that inputs from prompts are technically strings and should be converted to numbers if that is what you want.
x=prompt('What is your age?');
-
Arithmetic operations:
Addition: | x+y |
Subtraction: | x-y |
Multiplication: | x*y |
Division: | x/y |
Modulus: | x%y |
-
Boolean operations:
And: | x && y |
Or: | x || y |
Not: | !x |
-
String concatenation: The code below will assign the string 'The end.' to the variable
s
.
var a='The';
var b='end.';
var s=a+' '+b;
-
Boolean comparisons:
Equal: | x == y (double equal) |
Not equal: | x!=y |
Less than: | x<y |
Less than or equal: | x<=y |
Greater than: | x>y |
Greater than or equal: | x>=y |
-
Math functions: Here is a list of all JavaScript math functions. These are usually accessed through the Math object with a call such as
Math.sqrt(16)
. However, the editor provides quick access to these functions with calls such as sqrt(16)
.
The list: abs, acos, acosh, asin, asinh, atan, atanh, atan2, ceil, cbrt, expm1, clz32, cos, cosh, exp, floor, fround, hypot, imul, log, log1p, log2, log10, max, min, pow, random, round, sign, sin, sinh, sqrt, tan, tanh, trunc, E, LN10, LN2, LOG10E, LOG2E, PI, SQRT1_2, SQRT2.
-
*Print: (This is a special helper function for this editor and not a standard JavaScript function.) Print a string
s
to the output div: print(s);
Each string printed to the output div begins with a new line. Note that the div is an HTML div. You can print valid HTML to it and style your output if you so like.
-
*Print a matrix: (This is a special helper function for this editor and not a standard JavaScript function.) If
a
is a matrix (two-dimensional array), this code will print it in a not-too-ugly way. printMatrix(a);
-
*Create a zero matrix: (This is a special helper function for this editor and not a standard JavaScript function.) Set
a
equal to a 5x6 matrix of zeros. a=zeroMatrix(5,6);
-
*Print a bar chart: (This is a special helper function for this editor and not a standard JavaScript function.) Draw a bar chart with labels in an array
labels
an counts in an array counts
.
var labels=['red', 'blue', 'green'];
var counts=[3,7,5];
drawBarChart(labels, counts);
This will output: