JavaScript Cheatsheet
Tags: codenewbie, javascript
Published: 2020-08-17 06:26:47

If you're like me and find yourself trying to gain a better understanding of algorithms, you've come to the right place. Why do I need to know them, you ask? That is outside the scope of this post, but there are lots of articles out there on the subject. At the end of the day: you just need to know them. 

So as I go on my pilgrimage of algorithm enlightenment, I have found myself frequently going back to certain things and needing to look them up online. Often, these things will be basic parts of the language, but under the pressure of finding a solution to an algorithm, I find my mind going blank. To overcome that, I've come up with a list functions and a quick  description of what it does to help jog my memory. Here it is for you too in case you need that lightbulb to go off.

I will note that these are meant to be quick notes, not a full summary of what the function can do. Always read the documentation to fully understand what you are using!  

[array.pop()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop) => removes the **last** element of an array

[array.shift()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift) => removes the **first** element of an array

[array.push()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push) => adds element(s) to the **end** of an array

[array.unshift()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift) => adds element(s) to the **beginning** of an array

[array.slice()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice) => cut at a starting index, and optionally an end index. Original array **not** modified

[array.splice()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice) => changes contents of array in place
  * const a = "index to start at"
  * const b = "number of elements to be removed"
  * const c = "element(s) to be placed"
  * => to insert: .splice(a, b, c)
  * => to delete: .splice(a, b)
  * => to replace: .splice(a, 1, c)

[string.split('')](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split) => string to array of string elements

[array.join('')](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join) => array to string of array elements

[parseInt(stringThatRepresentsNumber)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt) => string to number

[Math.max(num1, num2, ...)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max) => find largest number 
[Math.min(num1, num2, ...)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/min) => find smallest number
[Math.sqrt(num)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sqrt) => square root of num
[Math.pow(num, power)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/pow) => num to power x
[Math.ceil(num)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/ceil) => rounds up
[Math.floor(num)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor) => rounds down

[[...new Set(array)]](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set) => creates an array with unique elements

[array.sort((a,b)=> a-b)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) => appropriately sorts an array of numbers

Need to get each individual digit of an array? 

```javascript
     while(num > 0) {
          last_digit = num % 10
          console.log(last_digit)
          num = Math.floor(num / 10)
     }
```

I am sure there are plenty more helpful bits out there, but these are the ones I run into most frequently. Have any other "go-tos" that I didn't list here? Add a comment with them and we can keep this list growing!