JavaScript Data Types

JavaScript variables can hold many data types: numbers, strings, objects etc.

var length = 16;                               // Number
var lastName = "Daniel";                      // String
var x = {firstName:"Emeka", lastName:"Emma"};    // Object

The Concept of Data Types

In programming, data types is an important concept.

To be able to operate on variables, it is important to know something about the type.

Without data types, a computer cannot safely solve this:

var x = 12 + "toyota"; 
var x = "toyota" + 12;

JavaScript evaluates expressions from left to right. Different sequences can produce different results:

var x = 12 + 8 + "toyota";
var x = "toyota" + 12 + 8;

JavaScript Booleans

Booleans can only have two values: true or false.

Example
var x = 5;
var y = 5;
var z = 6;
(x == y)       // Returns true
(x == z)       // Returns false 

JavaScript Arrays

JavaScript arrays are written with square brackets.

Array items are separated by commas.

The following code declares (creates) an array called cars, containing three items (car names):

Example
var cars = ["Benz", "Audi", "Toyota"]; 

Array indexes are zero-based, which means the first item is [0], second is [1], and so on.

JavaScript Objects

JavaScript objects are written with curly braces {}.

Object properties are written as name:value pairs, separated by commas.

Example
var person = {firstName:"Emeka", lastName:"Emma", age:33, eyeColor:"black"};
typeof ""             // Returns "string"
typeof "Emeka"         // Returns "string" 
Example
typeof 0              // Returns "number"
typeof 22            // Returns "number"
typeof (22)           // Returns "number"
typeof {name:Emeka, age:32} // Returns "object"
typeof [5,4,3,2]             // Returns "object" (not "array", see note below)
typeof null                  // Returns "object"
typeof function myFunc(){}   // Returns "function"

Modify Array Data With Indexes

var ourArray = [50,40,30];
ourArray[0] = 15; // equals [15,40,30]

Access Multi-Dimensional Arrays With Indexes

var arr = [
  [1,2,3],
  [4,5,6],
  [7,8,9],
  [[10,11,12], 13, 14]
];
arr[3]; // equals [[10,11,12], 13, 14]
arr[3][0]; // equals [10,11,12]
arr[3][0][1]; // equals 11

var arr = [1,2,3];
arr.push(4);
// arr is now [1,2,3,4]
var ourArray = ["Stimpson", "J", "cat"];
ourArray.push(["happy", "joy"]); 
// ourArray now equals ["Stimpson", "J", "cat", ["happy", "joy"]]

Manipulate Arrays With pop()

Another way to change the data in an array is with the .pop() function.

.pop() is used to “pop” a value off of the end of an array. We can store this “popped off” value by assigning it to a variable. In other words, .pop() removes the last element from an array and returns that element.

Any type of entry can be “popped” off of an array – numbers, strings, even nested arrays.

var threeArr = [1, 4, 6];
var oneDown = threeArr.pop();
console.log(oneDown); // Returns 6
console.log(threeArr); // Returns [1, 4]

Manipulate Arrays With shift()

pop() always removes the last element of an array. What if you want to remove the first?

That’s where .shift() comes in. It works just like .pop(), except it removes the first element instead of the last.

var ourArray = ["Stimpson", "J", ["cat"]];
var removedFromOurArray = ourArray.shift();
// removedFromOurArray now equals "Stimpson" and ourArray now equals ["J", ["cat"]].

Manipulate Arrays With unshift()

Not only can you shift elements off of the beginning of an array, you can also unshift elements to the beginning of an array i.e. add elements in front of the array.

.unshift() works exactly like .push(), but instead of adding the element at the end of the array, unshift() adds the element at the beginning of the array.

var ourArray = ["Stimpson", "J", "cat"];
ourArray.shift(); // ourArray now equals ["J", "cat"]
ourArray.unshift("Happy"); 
// ourArray now equals ["Happy", "J", "cat"]