I am one of those persons who learned React before properly learning the basic concepts of javascript. Because of this, in the early days of my React journey, I didn't know which part of the code is React and which part is the vanilla js. It's important to know the basic javascript concepts to understand better which part of the puzzle is React solving. In this blog post, I will be writing about different concepts of javascript that you see yourself using very often while working with React. It's better to know these before you deep dive into learning React. Logical AND (&&) and Logical OR (||) Operators Logical AND (&&) Operator Let's say we have the following expression - where and are . b c expressions b && c This will be evaluated to the value of only if is , otherwise, it will be evaluated to the value of c b truthy b Notes: If is , then the expression is not even going to be evaluated. b falsy c This is called . shortcut evaluation This will be used quite a lot while using React. Logical OR (||) Operator Let's say we have the following expression - where and are b c expressions b || c This will be evaluated to the value of if b is , otherwise, it will be evaluated to the value of . b truthy c Note: Shortcut evaluation happens here too. If is , then the expression is not even going to be evaluated. b truthy c You will be using this also quite often while using React. Template Literals This is a new ES6 way to create strings. Let's see an example. Assume that you want to create the following type of strings: 3 blog posts were written by Bhanu Teja in a span of 2 weeks. You will be given (number of blogs), (name of the user), (time span it took) as variables. count name span Without using template literals count = user = span = result = count + + name + + span + const 3 const 'Bhanu Teja' const 2 const ' blog posts were written by ' ' in a span of ' ' weeks.' Using template literals count = name = span = result = const 3 const 'Bhanu Teja' const 2 const ` blog posts were written by in a span of weeks.` ${count} ${name} ${span} Template literals start and end with a and you can write strings of text inside them and you have to wrap the around with and backtick(`) javascript expressions ${ } Let's add another use-case to the above example. If we have just 1 blog post you have to use instead of blog post blog posts If the time span is just 1 week, you have to use instead of . week weeks Without using template literals { (count === ) { text } text + } result = count + + pluralize( , count) + + name + + span + + pluralize( , span) + ( ) function pluralize text, count if 1 return return 's' const ' ' 'blog post' ' were written by ' ' in a span of ' ' ' 'week' '.' Using template literals { (count === ) { text } text + } result = ( ) function pluralize text, count if 1 return return 's' const ` were written by in a span of .` ${count} ${pluralize( , count)} 'blog post' ${name} ${span} ${pluralize( , span)} 'week' Ternary Operator This is a shorthand representation of the if-else statements. This is best explained using an example. (condition) { doSomething() } { doSomethingElse() } if else The above example when written using ternary operator condition ? doSomething() : doSomethingElse() Syntax condition ? expressionIfTrue : expressionIfFalse Shorthand Property Names id = name = count = user = { : id, : count, : name, } user = { id, : count, name, } const 2 const 'Bhanu' const 2 // This is the normal way const id blogs name // Using shorthand property names const blogs If the name of the and the name of the of the object are the same, then you can just write the variable name and omit the rest. variable property This is one of the things that I did not know when I was initially learning React, and you usually see this being used a lot in code and documentation. Object Destructuring This is a short-hand way to get the properties of an object into variables. user = { : , : , : } name = user.name blogs = user.blogs timeSpan = user.timeSpan { name, blogs, timeSpan } = user // we have a `user` variable that looks like this const name 'Bhanu Teja' blogs 3 timeSpan 2. // without using object destructuring const const const // using object destructuring const Note: The name of the destructured variables should be same as the name of the object properties. Array Destructuring This is a short-hand way to get the elements of an array into variables. name = [ , ] firstName = name[ ] lastName = name[ ] [firstName, lastName] = name // we have a `name` variable that looks like this const 'Bhanu Teja' 'P' // without using array destructuring const 0 const 1 // using array destructuring const Default Parameters You often want the function parameters to take some default values if that is not passed while calling the function. Let's see an example { a + b } sum( , ) sum( ) sum() ( ) function sum a = , b = 2 5 return 5 7 // a = 5, b = 7, result = 12 4 // a = 4, b = 5(default value of b), result = 9 // a = 2(default a), b = 5(default b), result = 7 So, whenever you want a parameter to take a default value, simply add a sign after the parameter and add your default value there. = Optional Chaining This is a relatively new feature of javascript. The optional chaining operator (?.) permits reading the value of a property located deep within a chain of connected objects without having to expressly validate that each reference in the chain is valid. - Taken from MDN Docs Consider the expression a?.b This expression evaluates to if is and , otherwise, it evaluates to . a.b a not null not undefined undefined You can even chain this multiple times like a?.b?.c If is or , then this expression evaluates to a undefined null undefined Else if is undefined or , then this expression evaluates to b null undefined Else this evaluates to a.b.c Syntax: obj.val?.prop obj.val?.[expr] obj.arr?.[index] obj.func?.(args) Nullish Coalescing Operator The nullish coalescing operator (??) is a logical operator that returns its right-hand side operand when its left-hand side operand is or , and otherwise returns its left-hand side operand. - Taken from null undefined MDN Docs Consider the expression a ?? b This evaluates to if is or , otherwise, it evaluates to b a null undefined a Spread Operator This operator spreads the values of an iterable object. Array Spread a = [ , , ] b = [ , ] .log(...a) c = [ , ...a, , ...b] .log(c) const 1 2 3 const 5 6 console // 1 2 3 // Now, if you want to have an array with values 0, 1, 2, 3, 4, 5, 6 const 0 4 console // 0 1 2 3 4 5 6 Object Spread first = { : , : } second = { : } result = {...first, ...second, : } .log(result) const a 1 b 2 const c 3 // Now to create an object {a: 1, b: 2, c: 3, d: 4} const d 4 console // {a: 1, b: 2, c: 3, d: 4} To learn more about spread operator, Check out this amazing article ES6 - Spread Operator Rest Operator The rest parameter syntax allows us to represent an indefinite number of arguments as an array. - Taken from MDN Docs Function Arguments { } sum( , , , , , ) ( ) function sum a, b, ...rest // ... 1 2 3 4 5 6 // a = 1, b = 2, rest = [3, 4, 5, 6] Usage with objects user = { : , : , : , } {name, ...rest} = user .log(name) .log(rest) const name 'Bhanu Teja' blogs 3 span 2 const console // Bhanu Teja console // { blogs: 3, span: 2} Arrow Functions This is a new ES6 way to write functions. sum = { a + b } { a + b } sum = { a + b } sum = a+ b multiplyBy2 = a * (or) multiplyBy2 = a * // without using arrow functions const ( ) function a, b return // (or) ( ) function sum a, b return // Using arrow functions const ( ) => a, b return // (or) const ( ) => a, b const ( ) => a 2 const => a 2 As you can see from the above example, converting the normal function to arrow functions can be done as follows: Remove the keyword. function Add an after the parameters. => Note If the body of the function is a simple expression you can even omit the keyword and also need not wrap it between and return { } If there is only one argument, you have the option to remove parenthesis around the arguments. There are still some more differences between arrow functions and normal functions, Checkout the following amazing articles to know more. A Simple Guide To Arrow Functions ES6 => Arrow Functions Array Methods There are so many array methods, but we frequently use some of these. I will be covering the following array methods. map filter reduce sort includes slice splice Array map() Method This method creates a new array from an existing array by calling a function for every element of the array. I always remember this as . mapping the values in an array to some other values Let's see an example. names = [ { : , : }, { : , : }, { : , : }, ] { name.firstName + + name.lastName } .log(names.map(callback)) names.map( { name.firstName + + name.lastName }) names.map( ) names.map( ) names.map( ) const firstName 'Bhanu Teja' lastName 'P' firstName 'Florin' lastName 'Pop' firstName 'Brad' lastName 'Traversy' // Let's say we have to create a new array with full names. // First let's write a callback function which takes an array element as an argument. ( ) function callback name return ' ' // Now let's call .map() method on the array console // ["Bhanu Teja P", "Florin Pop", "Brad Traversy"] // You can even inline the callback function which is how most of the people write this. ( ) function name return ' ' // Let's write the same using arrow functions and template literals that we just learned earlier ( ) => name ` ` ${name.firstName} ${name.lastName} // You can even omit the parenthesis around name => name ` ` ${name.firstName} ${name.lastName} // Let's use object destructuring ( ) => {firstName, lastName} ` ` ${firstName} ${lastName} Syntax: values.map(callback) // callback takes a single array element as an argument. // values is an array Note: Calling this method will not change the original array Array filter() Method Now that we know the method, it's easy to understand other array methods. They all have a similar syntax. Array map The array filter method creates a new array with elements that satisfy some given criteria. I always remember this as the method filters out elements that do not satisfy the criteria. filter users = [ { : , : }, { : , : }, { : , : }, { : , : }, { : , : }, ] users.filter( user.posts >= ) // consider the following array of users const id 1 posts 2 id 2 posts 1 id 3 posts 5 id 4 posts 4 id 5 posts 3 // Let's say we want to have all users who have at least 3 posts. ( ) => user 3 // [{id: 3, posts: 5}, {id: 4, posts: 4}, {id: 5, posts: 3}] Syntax: values.filter(callback) // callback takes a single array element as an argument. // values is an array Note: Calling this method will not change the original array Array reduce() method The array reduce method reduces the array of values into a single value. It executes the callback function for each value of the array. Lets see the syntax of the reduce method before seeing an example. array.reduce( ( ), ) function totalValue, currentValue, currentIndex, arr initialValue values = [ , , , , ] total = ( i = ; i < values.length; i++) { value = values[i] total = total + value } .log(total) initialValue = values.reduce( total + currentValue, initialValue) const 2 4 6 7 8 // Let's say that we want to have a sum of all these values. // Let's see how we do it using a traditional for loop let 0 for let 0 const console // Let's use reduce method now const 0 ( ) => total, currentValue Notes: is an optional parameter. initialValue Calling this method will not change the original array Array sort() method The callback function takes two different values as arguments. Based on the return value of the callback function, the two elements' positions are decided. If the return value is negative, then the first value is considered to be before the second value. If the return value is zero, then there will be no change in the order of the values. If the return value is positive, then the first value is considered to be after the second value. values = [ , , , , ] values.sort( { (a > b) { } (a < b) { } }) values.sort( b - a) const 4 10 2 1 55 // Let's say we want to sort this array in descending order // so if a and b are given // a should be before b if a > b // a should be after b if a < b // a and b can be at their own places if a === b ( ) => a, b if return -1 if return 1 return 0 // [55, 10, 4, 2, 1] // You can also do this as follows ( ) => a, b Note: The return value of the function is the sorted array This changes the original array If you do not pass any callback function, this sorts the values as strings and in ascending order. Array includes() Method This returns if the element is included in the array, otherwise returns . true false Syntax: array.includes(element) values = [ , , ] values.includes( ) values.includes( ) const 2 3 4 1 // false 2 // true Note: You can pass an optional parameter specifying the start index to start the search from. array.includes(element, startIndex) Array slice() method Syntax array.slice(start, end) Array slice will return the elements in the given range. start starting index to select the elements from This is an optional parameter and by default, it takes the value of 0 You can even pass a negative number. Negative number represents the position from the end. refers to the last element of the array, refers to last but one element, and so on. -1 -2 end ending index up to where to select the elements This is an optional parameter. If this is not passed, then all elements up to the end of the array will be selected. The element at will not be selected end This also accepts a negative number as an argument and the meaning is the same as before. numbers = [ , , , , , , , ] .log(numbers.slice()) .log(numbers.slice( )) .log(numbers.slice( , )) .log(numbers.slice( )) .log(numbers.slice( )) .log(numbers.slice( , )) const 0 1 2 3 4 5 6 7 console // [0, 1, 2, 3, 4, 5, 6, 7] console 2 // [2, 3, 4, 5, 6, 7] console 2 6 // [2, 3, 4, 5] console -1 // [7] console -3 // [5, 6, 7] console -3 -1 // [5, 6] Note: This doesn't change the original array Array splice() method Syntax: array.splice(index, count, item1, ....., itemX) This method is used to add or remove elements in an array. index The index at which the elements need to be added or removed. Can be a negative value too. count The number of elements to remove. item1, ....., itemX Items that will be added at index numbers = [ , , , , , ] numbers.splice( , , , , ) .log(numbers) const 0 1 2 100 6 7 // let's say we have to convert this array to [0, 1, 2, 3, 4, 5, 6, 7] 3 1 3 4 5 console // [0, 1, 2, 3, 4, 5, 6, 7] Note: The return value of the splice method is the array of items removed. This changes the original array To know more about different array methods, check out the amazing series Javascript Array Methods Default Exports vs Named Exports You often see yourself using ES Modules imports and exports while working with React. It's important to know how to import them when they are exported as default exports vs when they are exported as named exports. Check out the following amazing articles to read about these. JavaScript Modules and how to effectively work with Export Import ES Modules Promises You also need to have a basic knowledge of what promises are and how to work them. They will be used quite often in React. Check out to know more about them. this article Basic DOM Document APIs It is also good to be familiar with basic Document Apis like , etc. If you know these, you will appreciate the similarity and differences between React APIs and Document APIs. createElement getElementById If you are working with javascript for a while now, it is most likely that you already know basic Document APIs. MDN Docs are the best place to read up on these. Document - Web APIs | MDN You might also like the following React articles that I wrote: My Review of Kent C. Dodds's EpicReact.Dev: Introduction Javascript You Need To Know For React Intro to React Raw APIs Understanding JSX Creating Custom Components Styling And Handling Forms Managing State With useState Hook How to Create a Reusable LocalStorage Hook Easily Detect Outside Click Using useRef Hook The Lifecycle of React Hooks Component Previously published at https://blog.bhanuteja.dev/epic-react-javascript-you-need-to-know-for-react