JavaScript has several key built in data types - it's extremely important to know about them and what they are...
Unlike many programming languages, JavaScript has only one Number data type which handles all arithmetic and numerical values - unlike the usual approach of having different types for integer, floating point, and double precision numbers. Other types can be typecast to a Number type, for example for use in calculations, simply by inserting a + before their variable name. This makes JavaScript arithmetic very simple to learn, as demonstrated below...
var numberOne = 1;
var numberTwo = 2;
var numberString = "3";
/* note the + before numberString, which performs a type conversion -
making it a Number type rather than a String type. */
var total = numberOne + numberTwo + +numberString;
JavaScript has a String type, which can be created using the double-quote literal syntax. The String type can be concatenated using the + operator, and has several useful functions as discussed in the language reference here. The String type can hold any unicode character sequence. To convert other types to strings, you can either use String(myObject), or call myObject.toString() (where myObject is the object you want to convert).
// String literals simply use double quotes...
var hello = "Hello";
var world = "World";
var number = 5;
// strings can be concatenated using the + operator...
var result = hello + " " + world + " " + String(number);
// and printed straight to the console...
console.log(result);
Either myObject.toString() or String(myObject) can be used to convert any variable to a String - however, use of .toString() can create an error if the variable you call it on is nil - the use of String() avoids this possible issue.
Arrays allow many objects to be stored in one - like a 'list' of items. They are zero-indexed in JavaScript, meaning that the first item in the array is at position 0. Arrays can be created using the [] literal syntax (or created full of objects like [object1, object2, object3]). Items can be added to the array using 'bracket syntax' - so myArray[2] = myObject sets object 3 in myArray to be myObject, and myObject = myArray[2] sets myObject to be equal to the 3rd object in myArray.
var blankArray = [];
var fullArray = ["Hello", "How are you today?", "JavaScript is the best!"];
// how big is the full array?
var arraySize = fullArray.length;
// copy items from the fullArray into the blankArray...
for (var i = 0; i < arraySize; i++) {
blankArray[i] = fullArray[i];
}
// now let's print out the not-so-blank contents of blankArray
// (along with some info on where things were...)
for (var i = 0; i < blankArray.length; i++) {
var stringToPrint = blankArray[i];
console.log(stringToPrint + " was at index " + String(i));
}
Array size can be accessed through myArray.length - arrays also feature built in sorting methods which can be very useful - more on those here...
JavaScript uses objects heavily. They're a key-value based data type - similar to Dictionaries in Python, or Maps in Java - where the object's data values are held with a corresponding key. Objects can be created using the convenient { myKey: myValue } literal syntax, and then values can be accessed via dot notation, as in the example below...
// let's create a literal object...
var myObject = {
keyOne: "valueOne",
keyTwo: 5,
keyThree: ["hi", "hello"]
};
// let's get a value from the object...
var myString = myObject.keyOne;
// and now set a value...
myObject.keyTwo = 7;
// keys can be added dynamically too...
myObject.anotherKey = "another value";
// let's check what the object now contains...
console.log(myObject);
Because JavaScript supports functions as variables, as we'll discuss in the next section, objects can easily be given methods too.