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.
xxxxxxxxxx41> a = 5253> a45var: Variables can be declared with var.
MUTABLE
Not block scoped, but function scoped.
xxxxxxxxxx51> var b = "hello"2undefined3> b4'hello'5let: A newer ES6 variable that acts almost the exact same as var:
xxxxxxxxxx61> let c = 422undefined3> c4425> 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. xxxxxxxxxx71> const d = "Stay gold"2undefined3> d4'Stay gold'5> d = 36Thrown:7TypeError: Assignment to constant variable.{}s)xxxxxxxxxx131function mycoolFunction(){2 if(true){3 var funkyVar = 'rhombus' //exist in function scope4 const blockyConst = 'cube' //exist in block scope5 let blockyLet = 'rectangular prism' //exist in block scope67 }8 console.log(funkyVar)9 console.log(blockyConst)10 console.log(blockyLet)11}1213mycoolFunction()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
xxxxxxxxxx71const 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!
xxxxxxxxxx131 a = 52 var b = 'whatever'3 let c = 32.64 const d = false5 const e = "stuff"67console.log(`a: `, typeof a)8console.log(`b: `, typeof b)9console.log(`c: `, typeof c)10console.log(`d: `, typeof d)11console.log(`e: `, typeof e)1213All non-primitive types are object types.
An object is a datatype that contains key-value pairings.
xxxxxxxxxx81const someObject = {2 keyNameOne: "SomeValue", 3 keyNameTwo: 23, 4 keyNameArray: [1,2,"three"]5 keyNameFour: {6 subObjectKey: 3.27 }8}To access these elements, you need only to use dot notation:
xxxxxxxxxx11console.log(someObject.keyNameFour.subObjectKey)xxxxxxxxxx181 class SpiceGirl {2 constructor(spice){3 this.spice = spice4 this.name = spice + ' spice'5 }6 7 getSpicy() {8 return "SPICE UP YOUR LIFE with " + this.spice9 }10 11 get name(){12 return this.name13 }14 15 set spice(value){16 this.spice = value17 }18 }Just like other languages try catch blocks exist to handle errors gracefully.
xxxxxxxxxx61try {2 // do something bad3 const notPossible = 1/04} catch (error) {5 console.error("There's an error!", error)6}Arrays are lists.
xxxxxxxxxx71 const myList = [1,2,3,4,5]2 const otherList = ['one','two','three']3 const thirdList = [1,'three', 23.4, otherList]45 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):
xxxxxxxxxx31function doStuff1(thing) {2 console.log("We're doing stuff!", thing)3}Function Expressions: assignment to a variable
xxxxxxxxxx31 const doStuff2 = function(thing){2 console.log("We're doing stuff!", thing)3 }Named Function Expressions: (these play well with stack traces)
xxxxxxxxxx31 const doStuff3 = function doStuff(thing){2 console.log("We're doing stuff!", thing)3 }Arrow Functions: As of ES6, arrow functions have been introduced.
xxxxxxxxxx31 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.
xxxxxxxxxx31const 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:
xxxxxxxxxx141const formatSomeObject = (someObject, someParameter) => ({2 name: someObject.coolThing,3 date: someObject.time,4 hobby: someParameter5})67const myNeatObject = {8 coolThing: 'Mr Potato Head',9 time: '1996',10}1112const whatDoYouDo = "Jam eyes and things into a potato"1314const returnedObject = formatSomeObject(myNeatObject, whatDoYouDo)xxxxxxxxxx51 const doStuff5 = (thing='regularCoolThing', otherThing='superCoolThing') => {2 3 const stuff = thing + otherThing4 console.log("We're doing stuff!", stuff)5 }xxxxxxxxxx31 const doStuff6 = thing => {2 console.log("We're doing stuff!", thing)3 }xxxxxxxxxx91const doStuff7 = (thing='regularCoolThing', otherThing='superCoolThing') => {2 3 const stuff = thing + otherThing4 console.log("We're doing stuff!", stuff)5}67const argumentsArray = ['one', 'two']89doStuff7(argumentsArray)Functions can destructure an object to pass in arguments:
xxxxxxxxxx131const doStuff8 = ({ thing ='regularCoolThing', 2 otherThing='superCoolThing' }) => {3 4 const stuff = thing + otherThing5 console.log("We're doing stuff!", stuff)6}78const argumentsObject = {9 thing: 'Stuff', 10 otherThing: ' and things', 11}1213doStuff8(argumentsObject)xxxxxxxxxx51 const returnAnArrayToDestructure = () => {2 return ["Thing","other",3,4]3 }45 const [ first, second, third, fourth ] = returnAnArrayToDestructure()xxxxxxxxxx91 const returnAnObjectToDestructure = () => {2 return { 3 one: 2, 4 buckle: 'my', 5 shoe: "¯\_(ツ)_/¯", 6 }7 }8 9 const {one, buckle, shoe} = returnAnArrayToDestructure()xxxxxxxxxx131 const externalFunction = () => {2 const internalFunction = something => {3 return something + " nothing"4 }56 const stringToReturn = internalFunction("Something is not")7 8 return stringToReturn9 }1011 console.log(externalFunction())1213 console.log(internalFunction())xxxxxxxxxx11- Because objects can have anything as a key value pairing, objects can have functions paired with a key:
xxxxxxxxxx121const vampire = {2 firstName: 'David', 3lastName: 'Boreanas', 4 pseudonym: 'Angel/Angelus',5printFullName: function() {6 console.log(this.firstName + ' ' + this.lastName)7 }8}910console.log(vampire.firstName + ' is a name')11console.log('His full name is ')12vampire.printFullName()function and an anonymous functionxxxxxxxxxx191 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 () xxxxxxxxxx91 const doStuff = () => {2 console.log("I'm doing stuff!")3 }45 const doSomething = something => {6 something()7 }89 doSomething(doStuff)xxxxxxxxxx101 const doStuffAgain = somethingCool => {2 console.log("I'm doing ", somethingCool)3 }45 const doSomethingAgain = something => {6 const someVariable = "really neat!"7 something(someVariable)8 }910 doSomethingAgain(doStuffAgain)xxxxxxxxxx111 const functionsReturningFunctions = () => {2 const someFunction = () => {3 console.log("RETURN TO ME!")4 }56 return someFunction7 }89 const returnedFunction = functionsReturningFunctions()1011 returnedFunction()xxxxxxxxxx111 const functionsReturningFunctions = () => {2 const someFunction = () => {3 console.log("RETURN TO ME!")4 }56 return someFunction7 }89 const returnedFunction = functionsReturningFunctions()1011 returnedFunction()xxxxxxxxxx121 const functionsDoingCrazyStuff = () => {2 const someOtherFunction = someVariable => {3 console.log(`Some other function calls: `, someVariable)4 }56 return someOtherFunction7 }89 const returnedOtherFunction = functionsDoingCrazyStuff()1011 const someString = "HOW CRAZY!"12 returnedOtherFunction(someString)xxxxxxxxxx91 const crazyFunctionAgain = () => {2 const crazyOtherFunc = someVariable => {3 console.log(`Some other function calls: `, someVariable)4 }56 return crazyOtherFunc7 }89 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):
xxxxxxxxxx91const createAFunction = word => {2 const wordsToSay = "Well, I say " + word3 4 return () => console.log(wordsToSay)5}67const newFunction = createAFunction()89newFunction()