Skip to content

JavaScript Notes

JavaScript is a great web-development scripting language that has grown in popularity over the last few decades. Because of its ubiquity, I thought I should put my best foot forward and gain some basic knowledge in the language. Below are some of my notes! Thank you to Codecademy for your Introduction to JavaScript course. This is were I began my journey.

Variable

let variableName = variableValue; Use this for dynamic variables.

const variableName = variableValue; Use this for static variables and for declaring functions.

var variableName = variableValue; Kinda outdated.

Functions

function functionName(para1, para2) {function body} Fairly traditional declaration of function.

const functionName = function(para1, para2) {function body} Function Expression.

const functionName = () || para1 || (para1, para2) => {function body}; Arrow notation with simplified formatting.

Logical Operators

= -> assigning variables
=== -> checking equivalence
|| Or
&& And
! Not

Arrays

const || let arrayName = [item1, item2];

items within a const array are still mutable.

Functions for Arrays

.push(item) adds a item to the end of an array.

.pop() removes and returns the last item in an array.

.shift() removes the first item in an array.

.unshift(item1) adds item1 to the beginning of an array.

.slice(indexOfStart, indexOfEnd) returns items within that range of the array (indexOfEnd can be omitted).

.indexOf(item) returns the index of item within the array.

Array Iterators

arrayName.forEach(arrayItem/newFunctionName => {what you want to happen for each item}) Use arrayItem within the callback function. Returns undefined always.

const newArrayName = arrayName.map(arrayItem/newFunctionName => {return what you want to happen for each item}) Creates a new array based off of an old array.

const newArrayName = arrayName.filter(arrayItem/newFunctionName => {return what you want to happen for each item}) Create new array off of criteria in function and off old array. Returns true or false.

.findIndex returns index that match cirietia

.reduce = pointless addition

.every = returns boo if something matches crietia

Objects

let objectName = {
keyName: ‘value’,
‘key Name (withspeicalchar)’: ‘value }

objectName.keyName -> give the value assigned to it. do notation

~ or ~

objectName[‘keyName’] -> give the value assigned to it. Bracket notation. Use this when strings have spaces. MUST USE THE QUOTES!!

functions within an object = method
const objectName = {
methodName: function (para) {functionBody}
methodName (para) {functionBody}
methodName: (para) => {functionBody} Don’t use with this.!!}

Getters and Setters

get functionName(){
check to make sure values are expected
return value;}
Call it like a property and not like a method -> objectName.getMethodName;

set functionName(para){
check to make sure value is expected
assign it to the variable}
Call it like you’re setting a property -> objectName.setMethodName = value;

Short-Hand for Factory Objects (Property Value & Assigning Variables (Destructured Assignment)

const objectName = (para1, para2) => {
para1,
para2} assigns the matching name parameters to the keys

const {para1} = objectName; Assigns the value in objectName.para1 to the variable para1.

JS Object Methods

Object.keys(objectName) Returns array of all keys in objectName.

Object.entries(objectName) Returns array of all key-value pairs in objectName.

const newObject = Object.assign({objectName}, {newKey: newValue}) Creates newObject with the same properties as objectName with additional key-values.

Classes

class ClassName {
constructor(para1){
this.para1 = para1;
other vari}
get para1 () {
return para1;}
otherMethods(){
stuff and things}
static methodName(){
for methods to only be used by the class itself.}}

class ChildToClass extends Class {
constructor(para1, para2){
this._para2 = para2;
super(para1);}}

const newClassName = new objectName(para1);

Leave a Reply

Your email address will not be published. Required fields are marked *