Repository navigation
Javascript
- Introduction
- Variables
- Numbers and strings
- Objects, properties and Arrays
- Functions
- Arrow functions
- Classes
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 variable can be constant or changeable.
In this example, we define a variable called myBox and put a value of "apple" in it. The const word indicates that the variable's value won't change after it is created, it will be constant.
const myBox = "apple"In this example, a changable variable is created. The let word is used so we know the variable can be changed. Its value is first set to "banana", then changed to "tomatoes".
let myBag = "banana"
myBag = "tomatoes"Numbers are types of numerical data that can include decimals, for example
const a = 9.43
const b = 8
const c = 10001Strings are types of textual data that may be letters, words, sentences, etc.
const name = "ben"Javascript has this fun thing where you can do math with numbers and strings that can give surprising results. For example..
const a = 1;
const b = "9";
const c = a + b; // now c will be "19" (a string, not a number)Objects are collections of data that have a key:value format. The data items are referred to as "properties". Here's an example of a person object that has two properties, name and drives:
const 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:
const 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 [ ].
For great (too much?) detail about Objects and properties, try this Codeburst article.
Functions are special kinds of objects that ... do stuff. They can accept values as arguments (also known as parameters), and can return values. Return means that the result of the function gets handed back (returned) to whatever triggered (aka called) the function.
Here's an example of a function that does an addition of the two input values it receives, then returns the result.
function addValues(value1, value2) {
const 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.
const result = addValues(1,2)Fat arrow functions are shorter ways of writing functions, and have some neat magic powers.
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 following code defines a "Pet" class, then creates an instance of Pet with a starting value for the pet's name as "puppy". The new instance is stored as dog1 so we can refer to elsewhere. Run this code on JS Bin
class Pet {
constructor(name) {
this.name = name
}
}
const dog1 = new Pet("puppy")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...