Skip to content

Instantly share code, notes, and snippets.

@joshterrill
Last active April 27, 2018 02:42
Show Gist options
  • Save joshterrill/3c697f66823b2839653920a975c9c750 to your computer and use it in GitHub Desktop.
Save joshterrill/3c697f66823b2839653920a975c9c750 to your computer and use it in GitHub Desktop.
Javascript Programming Cheat Sheet

Programming Cheat Sheet

Javascript Types

  • Strings are defined by encompassing text inside of single or double quotes.
    • let myString = 'some string goes here'
  • Numbers are defined by creating a variable and putting a number in it. It can also have decimal points.
    • let myNumber = 443
  • Arrays are lists of things that are defined by putting items in between square brackets
    • let myArray = [1,2,3,4]
    • let myArray = ['josh', 'lucas', 'aree']
    • let myArray = [1, 2, 'josh', 'lucas']
    • let myArray = [{name: 'Josh', age: 25}, {name: 'Lucas', age: 19}]
  • Objects are containers of key value pairs. They are collections of other types of items, they can have strings, numbers, arrays, and other objects.
    • {name: 'Josh', age: 25, pets: ['Goose', 'Bella'], address: {city: 'Modesto', state: 'CA', street: '3804 Green jade court'}}

String methods

  • Strings can be turned into arrays using the .split() command. It takes one argument, which is what you want to split on. i.e.
let myString = 'my name is josh';
console.log(myString.split(' ')) // returns ['my', 'name', 'is', 'josh']
console.log(myString.split('my name ')) // returns ['', 'is josh']
  • You can check to see if a string exists in a string using the .includes() method. It takes one argument which is the string you want to check on. This returns a boolean, true or false.
let myString = 'this is my string';
console.log(myString.includes('this is)) // returns true
console.log(myString.includes('my string')) // returns true
console.log(myString.includes('josh')) // returns false

Object methods

  • Objects are defined by {} and have key value pairs in them. {key: value} i.e. {name: 'josh', age: 25}
  • Objects sometimes have child objects like this:
let josh = {
  name: 'Josh',
  age: 25,
  address: {
    street: '3804 Green Jade Court',
    city: 'Modesto',
    state: 'CA',
    zip: 95355
  }
}

In order to drill into an object and select child objects or child properties, you can do this by calling the object and then a . (period). i.e. to select the city property, you'd do josh.address.city - If you want to select the whole address object, you'd do josh.address

Array methods

  • You can find out the length of an arrray by typing arrayName.length i.e.
let myArray = [4, 4, 5, 2];
console.log(myArray.length) // returns 4
  • You can find multiple items inside of an array using the .filter() method. This is especially useful for finding objects by some criteria an array of objects.
let myArray = [
{
    name: 'Josh',
    gender: 'male'
}, {
    name: 'Lucas',
    gender: 'male'
}, {
    naem: 'Missy',
    gender: 'female'
}
];

let foundMales = myArray.filter((item) => {
    return item.gender === 'male';
});
let foundFemales = myArray.filter((item) => {
    return item.gender === 'female';
});
console.log(foundMales) // returns [{name: 'Josh', gender: 'male'}, {name: 'Lucas', gender: 'male'}]
console.log(foundFemales) // returns [{name: 'Missy', gender: 'female'}]
  • You can find a single item inside of an array using the .find() method. This is similar to the filter method, but it will only find the first item and returns the single item.
let myArray = [
{
    name: 'Josh',
    gender: 'male'
}, {
    name: 'Lucas',
    gender: 'male'
}, {
    naem: 'Missy',
    gender: 'female'
}
];

let foundMales = myArray.find((item) => {
    return item.gender === 'male';
});
let foundFemales = myArray.find((item) => {
    return item.gender === 'female';
});
console.log(foundMales) // returns {name: 'Josh', gender: 'male'}
console.log(foundFemales) // returns {name: 'Missy', gender: 'female'}
  • You can reverse an arrays items by calling .reverse() on it. i.e.
let myArray = ['josh', 'lucas', 'missy'];
console.log(myArray.reverse()) // returns ['missy', 'lucas', 'josh']
  • You can select any item in an array by selecting the index of it using square brackets []. i.e.
let myArray = [1, 2, 3, 4, 5];
let myOtherArray = [{name: 'josh', age: 25}, {name: 'lucas', age: 19}];
console.log(myArray[2]) // returns 3
console.log(myOtherArray[1]) // returns {name: 'lucas', age: 19}

Working with functions

  • Functions are created by notating them with a variable name followed by parenthesis and squiggly brackets.
myFunctionName() {
  // do stuff here
}
  • Functions can take incoming parameters that can be used in the function.
sayMyName(firstName, lastName) {
  console.log('Hello' + firstName + ' ' + lastName)
}

sayMyName('Josh', 'Terrill') // will log out 'Hello Josh Terrill'
  • Variables can be set equal to functions to have a value put in them as long as the function returns something.
fullName(firstName, lastName) {
  return firstName + ' '+ lastName;
}

let myName = fullName('Josh', 'Terrill');

console.log(myName) // returns 'Josh Terrill'
  • Functions can interact with variables outside of it as well and modify them in angular as well
myArray = [{name: 'josh', age: 25}, {name: 'lucas', age: 19}, {name: 'aree', age: 16}];

getSixteenYearOld() {
  return this.myArray.filter((item) => {
    return item.age === 16;
  });
}

console.log(getSixteenYearOld) // returns {name: 'aree', age: 16}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment