Javascript is written in unicode, which ultimately means that you can code with english, french, german, norwegian, hindi, tagalok, mandarin, and even emoji.
Semicolons:
When do you mutate, when do you not (hint: NEVER MUTATE ANYTHING)
Using nothing: In javascript, it is possible to assign a value to a variable.
xxxxxxxxxx
41> a = 5
25
3> a
45
var
: Variables can be declared with var.
MUTABLE
Not block scoped, but function scoped.
xxxxxxxxxx
51> var b = "hello"
2undefined
3> b
4'hello'
5
let
: A newer ES6 variable that acts almost the exact same as var
:
xxxxxxxxxx
61> let c = 42
2undefined
3> c
442
5> c = 'whatever'
6'whatever'
const
: Once initialized, a const
's value can never be changed.
IMMUTABLEish
const
means that the reference to the identifier can't be changed. const
for declaring their variables. xxxxxxxxxx
71> const d = "Stay gold"
2undefined
3> d
4'Stay gold'
5> d = 3
6Thrown:
7TypeError: Assignment to constant variable.
{}
s)xxxxxxxxxx
131function mycoolFunction(){
2 if(true){
3 var funkyVar = 'rhombus' //exist in function scope
4 const blockyConst = 'cube' //exist in block scope
5 let blockyLet = 'rectangular prism' //exist in block scope
6
7 }
8 console.log(funkyVar)
9 console.log(blockyConst)
10 console.log(blockyLet)
11}
12
13mycoolFunction()
Primitives:
Number
encompasses both floating points and integers
A sequence of characters. Strings can be enclosed in both single and double quotes, as well as ticks. As of ES6, template strings now exist (those with the ticks). Template stings allow for mid-string variable interpolation
xxxxxxxxxx
71const doubleHello = "HELLO"
2 const singleHello = 'Hello'
3const templateStringHello = `The above two sentences say
4 ${doubleHello}
5and
6 ${singleHello}
7 `
Booleans are true and false.
Javascript also distinguishes between truthy and falsey values.
truthy: Everything else!
Example!
xxxxxxxxxx
131 a = 5
2 var b = 'whatever'
3 let c = 32.6
4 const d = false
5 const e = "stuff"
6
7console.log(`a: `, typeof a)
8console.log(`b: `, typeof b)
9console.log(`c: `, typeof c)
10console.log(`d: `, typeof d)
11console.log(`e: `, typeof e)
12
13
All non-primitive types are object types.
An object is a datatype that contains key-value pairings.
xxxxxxxxxx
81const someObject = {
2 keyNameOne: "SomeValue",
3 keyNameTwo: 23,
4 keyNameArray: [1,2,"three"]
5 keyNameFour: {
6 subObjectKey: 3.2
7 }
8}
To access these elements, you need only to use dot notation:
xxxxxxxxxx
11console.log(someObject.keyNameFour.subObjectKey)
xxxxxxxxxx
181 class SpiceGirl {
2 constructor(spice){
3 this.spice = spice
4 this.name = spice + ' spice'
5 }
6
7 getSpicy() {
8 return "SPICE UP YOUR LIFE with " + this.spice
9 }
10
11 get name(){
12 return this.name
13 }
14
15 set spice(value){
16 this.spice = value
17 }
18 }
Just like other languages try catch blocks exist to handle errors gracefully.
xxxxxxxxxx
61try {
2 // do something bad
3 const notPossible = 1/0
4} catch (error) {
5 console.error("There's an error!", error)
6}
Arrays are lists.
xxxxxxxxxx
71 const myList = [1,2,3,4,5]
2 const otherList = ['one','two','three']
3 const thirdList = [1,'three', 23.4, otherList]
4
5 console.log(myList)
6 console.log(otherList)
7 console.log(thirdList)
Functions are objects. Function Objects. In order to use a function, it must be invoked with ()
. Otherwise, simply typing the function name with no parentheses refers to the object's reference.
Function Declarations (older):
xxxxxxxxxx
31function doStuff1(thing) {
2 console.log("We're doing stuff!", thing)
3}
Function Expressions: assignment to a variable
xxxxxxxxxx
31 const doStuff2 = function(thing){
2 console.log("We're doing stuff!", thing)
3 }
Named Function Expressions: (these play well with stack traces)
xxxxxxxxxx
31 const doStuff3 = function doStuff(thing){
2 console.log("We're doing stuff!", thing)
3 }
Arrow Functions: As of ES6, arrow functions have been introduced.
xxxxxxxxxx
31 const doStuff4 = (thing) => {
2 console.log("We're doing stuff!", thing)
3 }
Arrow functions allow for an implicit return for one line statements! This means that you don't have to write return variableName in specific instances.
xxxxxxxxxx
31const returnsHelloWorld = () => "helloWorld"
2const returnedString = returnsHelloWorld()
3console.log(returnedString)
When implicitly returning an object from an arrow function, the object needs to be wrapped in parentheses:
xxxxxxxxxx
141const formatSomeObject = (someObject, someParameter) => ({
2 name: someObject.coolThing,
3 date: someObject.time,
4 hobby: someParameter
5})
6
7const myNeatObject = {
8 coolThing: 'Mr Potato Head',
9 time: '1996',
10}
11
12const whatDoYouDo = "Jam eyes and things into a potato"
13
14const returnedObject = formatSomeObject(myNeatObject, whatDoYouDo)
xxxxxxxxxx
51 const doStuff5 = (thing='regularCoolThing', otherThing='superCoolThing') => {
2
3 const stuff = thing + otherThing
4 console.log("We're doing stuff!", stuff)
5 }
xxxxxxxxxx
31 const doStuff6 = thing => {
2 console.log("We're doing stuff!", thing)
3 }
xxxxxxxxxx
91const doStuff7 = (thing='regularCoolThing', otherThing='superCoolThing') => {
2
3 const stuff = thing + otherThing
4 console.log("We're doing stuff!", stuff)
5}
6
7const argumentsArray = ['one', 'two']
8
9doStuff7(argumentsArray)
Functions can destructure an object to pass in arguments:
xxxxxxxxxx
131const doStuff8 = ({ thing ='regularCoolThing',
2 otherThing='superCoolThing' }) => {
3
4 const stuff = thing + otherThing
5 console.log("We're doing stuff!", stuff)
6}
7
8const argumentsObject = {
9 thing: 'Stuff',
10 otherThing: ' and things',
11}
12
13doStuff8(argumentsObject)
xxxxxxxxxx
51 const returnAnArrayToDestructure = () => {
2 return ["Thing","other",3,4]
3 }
4
5 const [ first, second, third, fourth ] = returnAnArrayToDestructure()
xxxxxxxxxx
91 const returnAnObjectToDestructure = () => {
2 return {
3 one: 2,
4 buckle: 'my',
5 shoe: "¯\_(ツ)_/¯",
6 }
7 }
8
9 const {one, buckle, shoe} = returnAnArrayToDestructure()
xxxxxxxxxx
131 const externalFunction = () => {
2 const internalFunction = something => {
3 return something + " nothing"
4 }
5
6 const stringToReturn = internalFunction("Something is not")
7
8 return stringToReturn
9 }
10
11 console.log(externalFunction())
12
13 console.log(internalFunction())
xxxxxxxxxx
11- Because objects can have anything as a key value pairing, objects can have functions paired with a key:
xxxxxxxxxx
121const vampire = {
2 firstName: 'David',
3lastName: 'Boreanas',
4 pseudonym: 'Angel/Angelus',
5printFullName: function() {
6 console.log(this.firstName + ' ' + this.lastName)
7 }
8}
9
10console.log(vampire.firstName + ' is a name')
11console.log('His full name is ')
12vampire.printFullName()
function
and an anonymous function
xxxxxxxxxx
191 var human = {
2 firstName: 'Sarah',
3 middleName: 'Michelle',
4 lastName: 'Gellar',
5 pseudonym: 'Buffy Summers',
6 sobriquet: 'The Vampire Slayer',
7 printFullName: function() {
8 console.log(this.firstName + ' ' + this.middleName + ' ' + this.lastName)
9 },
10 printSobriquet: () => {
11 console.log(this.pseudonym + " " + this.sobriquet)
12 }
13 }
14
15 console.log(human.firstName + ' is a name')
16 console.log('Her full name is ')
17 human.printFullName()
18 console.log('Her character name is ')
19 human.printSobriquet()
The difference between the two function declarations is that the this
keyword interacts differently with each:
function() { ... }
: Refers to the object's enclosing object reference() => { ... }
refers to to the enclosing function context () xxxxxxxxxx
91 const doStuff = () => {
2 console.log("I'm doing stuff!")
3 }
4
5 const doSomething = something => {
6 something()
7 }
8
9 doSomething(doStuff)
xxxxxxxxxx
101 const doStuffAgain = somethingCool => {
2 console.log("I'm doing ", somethingCool)
3 }
4
5 const doSomethingAgain = something => {
6 const someVariable = "really neat!"
7 something(someVariable)
8 }
9
10 doSomethingAgain(doStuffAgain)
xxxxxxxxxx
111 const functionsReturningFunctions = () => {
2 const someFunction = () => {
3 console.log("RETURN TO ME!")
4 }
5
6 return someFunction
7 }
8
9 const returnedFunction = functionsReturningFunctions()
10
11 returnedFunction()
xxxxxxxxxx
111 const functionsReturningFunctions = () => {
2 const someFunction = () => {
3 console.log("RETURN TO ME!")
4 }
5
6 return someFunction
7 }
8
9 const returnedFunction = functionsReturningFunctions()
10
11 returnedFunction()
xxxxxxxxxx
121 const functionsDoingCrazyStuff = () => {
2 const someOtherFunction = someVariable => {
3 console.log(`Some other function calls: `, someVariable)
4 }
5
6 return someOtherFunction
7 }
8
9 const returnedOtherFunction = functionsDoingCrazyStuff()
10
11 const someString = "HOW CRAZY!"
12 returnedOtherFunction(someString)
xxxxxxxxxx
91 const crazyFunctionAgain = () => {
2 const crazyOtherFunc = someVariable => {
3 console.log(`Some other function calls: `, someVariable)
4 }
5
6 return crazyOtherFunc
7 }
8
9 crazyFunctionAgain()("DOUBLE PARENS!?")
When a function is run, it's executed within the scope of where it was defined, and not where it was run. (This ends up being incredibly helpful in the world of React):
xxxxxxxxxx
91const createAFunction = word => {
2 const wordsToSay = "Well, I say " + word
3
4 return () => console.log(wordsToSay)
5}
6
7const newFunction = createAFunction()
8
9newFunction()