10 JavaScript Concepts for React JS
10 JavaScript Concepts for React JS
Status Done
Var is the functional scope variable - which means we can use variable that
declare with var is accessible in whole function.
But let and const are the block scope variables - which means we can only use
them in that particular code block, in which they are defined.
Arrow Function
Arrow function is the another way to define function. By using arrow function
syntax we can define JavaScript functions more easily.
Section 2 Summary 1
const user = {
name: "Harley",
email: "[email protected]",
};
Object Destructuring
By using object destructuring, we can get properties as variables in just one line.\
const user = {
name: "Sam",
email: "[email protected]",
country: "US",
};
In map method, we have to pass callback function which runs for each item and
we can get that each item value in first parameter.
Now whatever we return from our call back function, it will add in new array.
Section 2 Summary 2
const displayItems = products.map((product) => `<li>${produ
ct}</li>`);
In this example, we get all arr1 and arr2 items using spread operator and add
them in our new array which is numbers array.
Section 2 Summary 3
We can also use this spread operator in objects for getting the objects values.
Ternary Operator
Ternary operator also known as conditional operator which is the shortcut way to
write if else condition.
Modules
Module is a file that contains code to perform specific task. It can contain
variables, objects, functions and so on.
Imagine we have 5 features in single file, then we can divide each feature in
single file called as module and then use them where we need it.
Now to use function in other modules, we need to export that function from that
module with export keyword.
// post.jsx
export function post() {
console.log("This is post");
}
// feed.jsx
import { post } from "./post";
Section 2 Summary 4
// main.jsx
import { feed } from "./feed";
feed();
Now we have another method to export function or variable from our module
which is by using export default
Mostly, we will use export our main function of our module as default export. The
difference is only in import statement.
// Named export
import { post } from "./post";
// Default export
import post from "./post";
Section 2 Summary 5