Javascript cheatsheet part-1
Tilak Khatri
Posted on December 4, 2022
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);
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]
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"]
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]
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
Example
const user = false;
user === true ? <h1>Your are welcome </h1> : <h2>Please log in here </h2>
// expected output: Please log in here.
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
}
function multiplyItems(x){
let resutl = x*2;
return result;
}
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
}
const res = (x) => x*2;
Your comments will appreciates.
For more programming and JavaScript tips be sure to follow. Thank you for reading!
Stay tuned..
Posted on December 4, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.