DEV Community

Cover image for Javascript cheatsheet part-1
Tilak Khatri
Tilak Khatri

Posted on

Javascript cheatsheet part-1

These are very important to remember and will help you become a better developer.
Start learning with following concepts.

Array Methods

By https://developer.mozilla.org/en-US/docs/Web/JavaScript/

The Array object, as with arrays in other programming languages, enables storing a collection of multiple items under a single variable name, and has members for performing common array operations.

These are most important array operation used during working.

Map

Map allows you to take an array and create a function to change each individual object in a certain array.

Example

let arrayExample = [1,2,3,4];
const newArray = arrayExample.map(x=>x*2);
console.log(newArray);
Enter fullscreen mode Exit fullscreen mode

Filter

Filter allows you to sort through the array and return all objects which follow the set of rules stated into a new array.

Example

let arrayExample = [14,25,33,48];
const newArray = arrayExample.filter(x=>x > 20);
console.log(newArray);

// exapected output: Array [25, 33, 48]
Enter fullscreen mode Exit fullscreen mode
const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];

const result = words.filter(word => word.length > 6);
console.log(result);
// expected output: Array ["exuberant", "destruction", "present"]
Enter fullscreen mode Exit fullscreen mode

Reverse

Reverse allows you to rearrange the array from the back to the front.

Example

let arrayExample = [14,25,33,48];
console.log(arrayExample.reverse());

// expected output: Array [48, 33, 25, 14]
Enter fullscreen mode Exit fullscreen mode

Ternary Operator

The ternary operator is sort of like an if statement, however the syntax is different. The ternary operator will check if a statement is true, and if so it will render the first statement. Else it will render the next statement.

Syntax

statement === true ? render this : else render this
Enter fullscreen mode Exit fullscreen mode

Example

const user = false;
user === true ? <h1>Your are welcome </h1> : <h2>Please log in here </h2>

// expected output: Please log in here.
Enter fullscreen mode Exit fullscreen mode

Think of the “?” as an if. If user is true , then render an h1. Else ( think of the “:” as else) render the h2.

Arrow Functions

Arrow functions allow you to remove many elements from the traditional function syntax and really clean up your code to make it less cluttered.

Traditional function
syntax

function function_name(args){
    //code
}
Enter fullscreen mode Exit fullscreen mode
function multiplyItems(x){
    let resutl = x*2;
    return result;
}
Enter fullscreen mode Exit fullscreen mode

Arrow functions allow you to remove the word “function” and the function name. You can also remove the parentheses from the input, however sometimes it is helpful to keep it as it makes reading the code
easier.
syntax

const var = (args) => {
    //code
}
Enter fullscreen mode Exit fullscreen mode
const res = (x) => x*2;
Enter fullscreen mode Exit fullscreen mode

Your comments will appreciates.
For more programming and JavaScript tips be sure to follow. Thank you for reading!
Stay tuned..

Top comments (0)