In this post I will share some running notes I’m compiling on JavaScript. I recommend the following free online resources for JavaScript. The first is a basic introduction, the second picks up where the first leaves off.
- Marijn Haverbeke’s Eloquent JavaScript
- Eric Elliott’s Programming JavaScript Applications
I’d also like to recommend another book which, like Elliott’s book on applications, is also a slightly higher entry point than Eloquent JavaScript.
- Dane Cameron’s A Software Engineer Learns HTML5, JavaScript and jQuery
Getting Started
If you download node then you can run JavaScript from the command line using the node
utility as
node script.js
To make sure everything is working properly, you should be able to save the following line as hello.js
, run node hello.js
from the command line, and see Hello, World.
as output.
console.log("Hello, World.") ;
Note the following,
console.log()
prints things to the standard output- statements should end in semicolons
- strings are delimited by double-quotes
As far as I can tell, we do not need to set our code off with a sh-bang line.
Types
JavaScript has several types:
- Number – everything is float by default
- String – uses single or double quotes
- Boolean – true, false
- Null – actually an object, exists for backwards compatibility
- Undefined – returned when you access a non-existent property or undeclared variable
- Object – all other data types including arrays and functions
Declaring Variables
Variables are declared using the var
keyword.
var foo = 0 ; var bar = 1 ; var baz ;
A best practice is to declare everything at once at the top using one var
keyword. This makes your code easier to read.
var foo = 0, bar = 1, baz ;
The above is more readable, but it creates a lot of global variables that might interact in an unexpected fashion with other components. You can prevent this behavior by wrapping all of your variables in an object, and using dot notation to access them.
var myNameSpace = { foo:0, bar:0, plus_one: function(x){ return x + 1 } } ; console.log( myNameSpace.foo ); console.log( myNameSpace.bar ); console.log( myNameSpace.plus_one( myNameSpace.bar ) ); console.log( myNameSpace.plus_one( 2 ) );
The console.log()
lines at the bottom should output 0,1,2,3
.
Operations on Variables
Exponents
On numerical data, addition, subtraction, multiplication, and division work as expected. Exponents are available through the Math.pow()
function. We can raise a base to integer or decimal value.
Math.pow( 9, 2 ) // nine squared --> 81 Math.pow( 9, 0.5 ) // square root of nine --> 3
Modulo Arithmetic
We can accomplish modulo arithmetic using the percent sign as shown in the ever popular FIZZBUZZ program,
function fizzbuzzer( x ) { if( ( x % 3 == 0 )&&( x % 5 == 0 ) ) { console.log(x+" FIZZBUZZ") ; } else if( x % 3 == 0 ) { console.log(x+" FIZZ") ; } else if( x % 5 == 0 ) { console.log(x+" BUZZ") ; } } ; for( var i=1 ; i <= 100 ; i++ ) { fizzbuzzer( i ) ; }
Scientific Notation
JavaScript also supports scientific notation so instead of writing one million as 1000000
, we can say 1e6
.
Control Structures
If Statements
Here is the simple if conditional. If you only have one line in the body of your conditional, then JavaScript allows you to omit the curly braces, but I find this confusing. I don’t enjoy using curly braces to delimit blocks of code, but I dislike inconsistency more, so I employ curly braces at every opportunity.
if( condition ) { stmts ; }
This is how I write my if-else and extended if statements. I’m not sure what the best practice is, but I sort of like this style.
if( condition ) { stmts ; } else { moar stmts ; }
if( condition ) { stmts ; } else if( condition ) { stmts ; } else { stmts ; }
Loops
For Loops
Here, we see that JavaScript loops look very much like C/++ loops. The index variable must be defined outside the loop, or inside the for call as shown here.
for( var i = 0 ; i < 10 ; i++ ) { console.log( i ) ; }
While Loops
while( condition ) { stmts ; }
Break
The break
keyword can be used to break out of for loops and while loops.
Arrays
Arrays are declared using square brackets, and elements are accessed via square brackets using integers starting at zero.
- length returns the length of the array
- push adds another element to the beginning of an array
- pop removes an element from the end of an array
- join joins the elements of an array into a string using a delimiter
Objects
Objects are declared using curly braces. Attributes or methods can be accessed using brackets or dot notation.
var obj = { foobar:2 }; console.log( obj["foobar"] ) ; console.log( obj.foobar ) ;
Functions
Functions may be defined as values using the var
keyword. Functions defined in this manner must precede their calls.
var functionName = function( args ) { stmts ; return output ; } ;
This is a function declaration, and it can be placed anywhere in the code.
function functionName( args ) { stmts ; return output ; } ;