Chapter 1: Types and variables

Javascript only has a couple of types that you really need to use. They are:

Number

String

Boolean

Function

Array

Object Literal

Comments

Add comments in your code like most other languages using // or /**/.

// a one line comment/* this is a longer, multi-line comment *//* You can't, however, /* nest comments */SyntaxError*/

Declaring variables

When you want to instantiate a variable, you should initiate them by using the 'var' keyword.

varnumber=123; // this is a Numbervarstring="hello"; // this is a Stringvarcondition=true; // true or falsevardoSomething=function () {}; // this is a functionvarobject= {}; // this is an objectvaridk; // undefined var, you can set it later

Variables can be any type. For example, if you decide that you want the "var number" to be a String after all, you can do so at any time. This is called dynamic typing, as opposed to static typing.

Note: if you forget to write "var", the variable you declare will be global. More on scopes later!

Number and String

Here are some examples of playing with Number and Strings:

// Cash is a numbervarcash=10;
// It could also be a string.// Note: you can use '' or "", it doesn't mattercash='$10';
cash="I have 10 dollars! I'm rich!!";
// Strings have methods and propertiescash="$10";
console.log(cash.length); // Output: 3// Numbers also have methods! For example you can use .toString()cash=10;
cash=cash.toString();
console.log(cash); // Output: "10"

Operators between Number and String

Using numbers and string should be pretty straight forward. Note that you can use operators on numbers and strings. Keep in mind that using + on Numbers and Strings will always result in strings. Other operators between Numbers and Strings result in Numbers.

// using + on a String and Number results in a stringcash='$'+10+","+21;
console.log(cash); // Output: "$10,21"// using * between String and Number results in a Numbercash=2*"10";
console.log(cash); // Output: 20// as long as the String is parsable as Numbers!cash="$"*10;
console.log(cash); // Output: NaN, aka Not A Number

Arrays

Arrays are data structures in which you can hold sequential data. It's simply a row of data, which can be Numbers, Strings, Booleans etc. Basically anything. Arrays can be instantiated using square brackets [].

Accessing the data

To access the array data, you use [] with an index. The index starts at 0.

vararray= [300, "hello", true];
// get the first data in array, starts with 0console.log(array[0]); // Output: 300// get the 2nd data in arrayconsole.log(array[1]); // Output: "hello"// or any other indexvarindex=2;
console.log(array[2]); // Output: true// an out of bounds index will not result in an error, instead will return undefinedconsole.log(array[3]); // Output: undefined// Use the length property to see how many elemnents the array hasconsole.log(array.length); // Output: 3

Editing the data

You can edit arrays easily. Just access the the element and assign something new:

Another thing to note: only Numbers, Strings and Booleans are compared by content. Whereas other types are compared by their referene.

varcat= {
name: "Nom Cat"
};
// Strings are compared by contentif (cat.name==="Nom Cat") {
// true!
}
// despite the contents being exactly the same, these objects are differentvarsameCat= {
name: "Nom Cat"
};
if (cat===sameCat) {
// false!
}
// this holds a reference to the first Object Literal, so this is the same! varanotherCat=cat;
if (cat===anotherCat) {
// these 2 cats point to the same Object Literal
}

Another unusual JavaScript feature (but very useful) is the concept of falsiness. Whereas in most languages it is required to pass a true/false (Boolean) to if-statements, it is sufficient in JavaScript to be "truth-y" or "falsy". The following are considered falsy:

false

undefined

null

0

NaN

an empty string ('' or "")

Anything else is considered to be truthy and could pass if-statements. Some examples: