Skip to content

Javascript

Ben Foley edited this page Jul 30, 2019 · 12 revisions

Javascript... so popular and yet??

It's been around a long time, and comes in and out of fashion. Seems like there's a new Javascript framework out each week. For the most part they come and go, some linger longer.

Fun fact: Javascript was created in 1995 at Netscape (showing your age if you know what Netscape is/was) Releases: v1 1995, v3 1999, v5 2009, v6 2015

Javascript's syntax is based on Java and C, but its like the wild child of two upstanding parents. While Javascript uses some of the structures (grammars) from Java and C, Javascript is looser, more easy going about things. It is loosely "typed", meaning for example that you can treat a number as though it is a word, "2" being equal to "two". This causes havoc in other languages. Types are categories of things, such as numbers, strings (words), dates. Typescript brings some rigourousness back to Javascript, preventing you from such doing such crazy wild things as equating "two" as being the same value as "2".

Some of the Javascript jargon that we will encounter are variables, numbers, strings, objects, arrays, functions and classes.

Variables are named placeholders for data. Upon creation a variables can be constant or changeable. More on this in a moment.

Numbers are types of numerical data that can include decimals, for example

let a = 9.43
let b = 8
let c = 10001

Strings are types of textual data that may be letters, words, sentences, etc.

let name = "ben"

Javascript has this fun thing where you can do math with numbers and strings that can give surprising results. For example..

let a = 1;
let b = "9";
let c = a + b; // now c will be "19" (a string, not a number)

Objects are collections of data that have a key:value format. Here's an example:

let person = {
    name: "ben",
    drives: "lotus"
}

Arrays are series of data. For some reason, fruits are commonly used in giving examples of arrays, so in the spirit of this tradition:

let fruits = ["mangosteen", "rambutan", "durian"]

Notice some differences between Object and Array notation? Objects have key:value data pairs and are wrapped in curly brackets { }. Arrays are lists of comma separated values wrapped in square brackets [ ].

Functions are special kinds of objects that ... do stuff. They can accept values as arguments (also known as parameters), and can return values. Here's an example of a function that does an addition of the two input values it receives.

function addValues(value1, value2) {
    let newValue = value1 + value2
    return newValue
}

To use this function, somewhere else in the code we would do something like the following, the expected result being the number 3.

addValues(1,2)

Classes are templates that can include default data and functions! For example we might want a "Pet" object, with properties of "species", "name", "maintenance", and some functions such as "nextFeedingTime()". Each instance of this class has the individual attributes of the particular pet and the class methods/functions.

The main thing to consider for classes is that instances of classes inherit their parent's properties and functions by using the "extends" syntax. We'll see this in React when we make our own code that extends React's Components. More on this soon...

Clone this wiki locally