Shreya Purohit | Data Analyst Profile picture
Threads on data science and analytics ๐Ÿ“ˆ

Sep 20, 2021, 36 tweets

One of the required prerequisites to learn React is ๐—˜๐—ฆ๐Ÿฒ.

๐‘‚๐‘›๐‘’ ๐‘ โ„Ž๐‘œ๐‘ข๐‘™๐‘‘ ๐‘˜๐‘›๐‘œ๐‘ค ๐ธ๐‘†6 ๐‘คโ„Ž๐‘’๐‘กโ„Ž๐‘’๐‘Ÿ ๐‘ฆ๐‘œ๐‘ข'๐‘Ÿ๐‘’ ๐‘™๐‘’๐‘Ž๐‘Ÿ๐‘›๐‘–๐‘›๐‘” ๐‘…๐‘’๐‘Ž๐‘๐‘ก ๐‘œ๐‘Ÿ ๐‘›๐‘œ๐‘ก.

๐Ÿงต This thread is all you need to know about ES6 before you get your hands on React:

#100DaysOfCode #javascript

โš›๏ธ You'll be able to learn React way better if you know working with ES6.

ES6 is a scripting language for JavaScript and helps developers by making the code simple as it handles out all the limitations of the main language (JS).
๐Ÿ‘‡

๐Ÿญ. ๐—ฆ๐—ฐ๐—ผ๐—ฝ๐—ฒ ๐—ผ๐—ณ ๐—ฉ๐—ฎ๐—ฟ๐—ถ๐—ฎ๐—ฏ๐—น๐—ฒ๐˜€ ๐—ถ๐—ป ๐—๐—ฆ
Before moving to the point, let us understand 3 concepts here:

1๏ธโƒฃ Global Scope - Variable is declared outside the function. This variable is accessible inside every function present in the code.

2๏ธโƒฃ Function Scope - Variable is declared inside (within) a function, outside that it is not accessible anywhere.

3๏ธโƒฃ Block Scope - Block scope means variables that are declared in a { } block are not accessible outside it. This block can be an if statement, for/while loop..

๐Ÿฎ. ๐—น๐—ฒ๐˜/๐—ฐ๐—ผ๐—ป๐˜€๐˜ ๐—ผ๐—ฟ ๐˜ƒ๐—ฎ๐—ฟ?

- var: function/ global scoped
var is both global and function scoped, which often creates a confusion. So avoid using it.

- let: block scoped.
โ†’ let keyword can be reassigned:

But when we use let inside a function, it works like:

Inside the function box() when we log the value of size, it shows a reference error. That is because let is block scoped.

- const: block scoped

โ†’ const is very similar to let except that they can't be changed and redeclared.

โ†’ therefore let and const are preferred over var keyword for declaring variables.

๐Ÿฏ. ๐—ข๐—ฏ๐—ท๐—ฒ๐—ฐ๐˜๐˜€

-> objects are written within curly braces { } as a collection of key: value pairs.

key: property name
value: value of that property

- Creating an empty object:

Talking specifically about ES6, before ES6 we had to specify both (key, value) even if both are of the same names.

ES6 helps us to get rid of duplication when we have the same key: value names. So now our code will look like this:

๐Ÿฐ. `๐˜๐—ต๐—ถ๐˜€` ๐—ธ๐—ฒ๐˜†๐˜„๐—ผ๐—ฟ๐—ฑ

this is a keyword. It basically returns a reference to the object it is placed within

๐Ÿ’ก NOTE:
When we call a function as a method in an object, this keyword returns a reference to that object. ๐Ÿ‘‡

But when we call the function alone, outside the object this returns the global object (browser window) and hence we get the result as undefined ๐Ÿ‘‡

๐Ÿฑ. ๐—”๐—ฟ๐—ฟ๐—ผ๐˜„ ๐—™๐˜‚๐—ป๐—ฐ๐˜๐—ถ๐—ผ๐—ป๐˜€

- Helps us to write functions in a much shorter, concise, and easier way.

- Having a single argument can remove the () around our parameters (num in below case)

๐Ÿฒ. ๐—ข๐—ฏ๐—ท๐—ฒ๐—ฐ๐˜ ๐——๐—ฒ๐˜€๐˜๐—ฟ๐˜‚๐—ฐ๐˜๐˜‚๐—ฟ๐—ถ๐—ป๐—ด

Let's say we have an object called girl such that it has 3 keys such as:

const girl = {
name: " ",
age: " ",
country: " ",
};

Normally, we would do something like this to get the values:

here, as you can see we have to repeat the object name girl every time we want to get a value. This problem can be solved by object destructuring:

๐‘๐‘œ๐‘›๐‘ ๐‘ก { ๐‘›๐‘Ž๐‘š๐‘’, ๐‘Ž๐‘”๐‘’, ๐‘๐‘œ๐‘ข๐‘›๐‘ก๐‘Ÿ๐‘ฆ } = ๐‘”๐‘–๐‘Ÿ๐‘™;

this one-line code works the same as the previous code.

So destructuring made our code shorter and easier to understand.

In case you want to use an alias (a different variable name) instead of key:

๐‘๐‘œ๐‘›๐‘ ๐‘ก {๐‘๐‘œ๐‘ข๐‘›๐‘ก๐‘Ÿ๐‘ฆ: ๐‘๐‘ก๐‘Ÿ๐‘ฆ} = ๐‘”๐‘–๐‘Ÿ๐‘™;

- This above line of code means we've defined a new variable called ctry = country.

๐Ÿณ. ๐—ฆ๐—ฝ๐—ฟ๐—ฒ๐—ฎ๐—ฑ ๐—ข๐—ฝ๐—ฒ๐—ฟ๐—ฎ๐˜๐—ผ๐—ฟ
- We use spread operator for adding items to an array, combining arrays/ objects, merging or cloning and array, and many more. Let's have a quick look at each of them:

Let's say we have two arrays such that

Case 1 - ๐ด๐‘Ÿ๐‘Ÿ๐‘Ž๐‘ฆ๐‘ 

๐ถ๐ด๐‘†๐ธ ๐ผ๐ผ - ๐‘‚๐‘๐‘—๐‘’๐‘๐‘ก๐‘ 

๐Ÿด. ๐—–๐—น๐—ฎ๐˜€๐˜€๐—ฒ๐˜€

-> they behave as a template for creating objects

Let us take an example of an object boy here. We have a function called run inside it.

Now if we've some bug in the future or we've to modify our function for a different object, it would be a long way.

- To overcome this and make our work easier, we use classes:

*Always add the constructor method when creating objects*

Now that we've created a class, let's try building our object again -

see it's that simple!

with this above class, we've implemented the run method in a single line of code. If someday we find a bug here, we've to modify it in just a single place {inside class Boy}. So this is the advantage of using classes in JS.

Now let's talk about Inheritance..

๐Ÿต. ๐—œ๐—ป๐—ต๐—ฒ๐—ฟ๐—ถ๐˜๐—ฎ๐—ป๐—ฐ๐—ฒ

-> when we acquire properties of one class into another class to extend it, it is inheritance.

If we have a class Boy such that -

..and we want to create another class (having similar properties + some specific properties of its own). We can do this using the keyword ๐‘’๐‘ฅ๐‘ก๐‘’๐‘›๐‘‘๐‘ 

we just created the class Girl here. Let us now create a const using this -

๐‘๐‘œ๐‘›๐‘ ๐‘ก ๐‘š๐‘ฆ๐บ๐‘–๐‘Ÿ๐‘™ = ๐‘›๐‘’๐‘ค ๐บ๐‘–๐‘Ÿ๐‘™("Sara");

and we're done. This code basically means that now the const myGirl will have the functions eat + run + constructor property of Boy class.

Now we can use it like below without writing a long piece of code:

myGirl. eat()
myGirl. run()

Now let's say we want to create another constructor inside the Girl class (derived class constructor).

We must call ๐‘ ๐‘ข๐‘๐‘’๐‘Ÿ() constructor inside the new constructor, otherwise, we'll get an error. Now, this must be looking confusing, let's look at the example below -

๐Ÿญ๐Ÿฌ. ๐— ๐—ผ๐—ฑ๐˜‚๐—น๐—ฒ๐˜€

At times, we can have many no. of classes declared in a single file. This makes the code long, confusing and messy.

To avoid this, we separate these classes into different files and import them as a module into the main file.
This is called modularity.

Let's look at it in action. Here's what our folder ๐‘ ๐‘Ÿ๐‘ will look like:

Here we created two files (boy, girl) inside ๐‘ ๐‘Ÿ๐‘ folder.

Doing this simple setup can make our code much easier to understand!

Why we used ๐‘–๐‘š๐‘๐‘œ๐‘Ÿ๐‘ก and ๐‘’๐‘ฅ๐‘๐‘œ๐‘Ÿ๐‘ก? ๐Ÿ‘‡

both Boy and Girl classes are private in the folder, in order to use them we made them public using the ๐’†๐’™๐’‘๐’๐’“๐’• keyword.

*We use ๐‘–๐‘š๐‘๐‘œ๐‘Ÿ๐‘ก keyword in line 1 of girl.js as it is an extended version of the Boy class.*

Now half of the work is done. For now, these classes are not accessible in our main app.js file. For that, we've to import them into our app.js file. We can do that by using -

๐Ÿญ๐Ÿญ. ๐——๐—ฒ๐—ณ๐—ฎ๐˜‚๐—น๐˜ ๐—ฎ๐—ป๐—ฑ ๐—ก๐—ฎ๐—บ๐—ฒ๐—ฑ ๐—˜๐˜…๐—ฝ๐—ผ๐—ฟ๐˜๐˜€

- Named Exports

We can export more than one object from a specific module. This is called named export. Eg:

Here we exported a class `Car` and a function `add`.๐Ÿ‘‡

- Default Exports

It is basically the main object that is exported from the module. It is generally used in case we've only a single object to export. Let's see how it is -

๐Ÿ’ก Now we don't need the import { Car } from "./car";

Instead, we use import Car from "./car"; in case of default exports.

I know this is too long to be called a thread and can be difficult to read, so I've compiled it in a blog. You can read it here:
dev.to/shreya/es6-hanโ€ฆ

๐Ÿ‘‹ Woosh! You've made it to the end. Hope I've helped you somehow.
If it did, ๐Ÿ” and โค๏ธ the thread and let's connect @eyeshreya :)

Share this Scrolly Tale with your friends.

A Scrolly Tale is a new way to read Twitter threads with a more visually immersive experience.
Discover more beautiful Scrolly Tales like this.

Keep scrolling