create account

Learn Es6 part#3 String literal, Shorthand, Destructuring object by duski.harahap

View this thread on: hive.blogpeakd.comecency.com
· @duski.harahap ·
$21.65
Learn Es6 part#3 String literal, Shorthand, Destructuring object
#### Repository
https://github.com/babel/babel

#### What Will I Learn?
- String literal
- Shorthand in object and function
- Destructuring object and assign value

#### Requirements
- Text editor
- Intermediate in javascript

#### Resources
- babel https://github.com/babel/babe


#### Difficulty
Basic

### Tutorial Content

This tutorial is a continuation tutorial from the es6 javascript tutorial series. we will learn more about es6 and know more about the benefits and usefulness of javascript in general. We will discuss topics that we often use and also we often practice on our projects or applications. I hope you have followed the previous tutorial so you are not confused. We will discuss two topics, namely ***string literals and destructing objects***. We just started this tutorial.

### String literal

There are several calls for this syntax, there are some that mention *literal templates, string literals or template strings*. But we will not discuss these differences, we will discuss how to use them and what are the differences if we use normal javascript.

- **Combine strings and variables**

For those of you who are familiar with javascript, they often combine strings and variables. string literals make it easier for us to combine these two things. we will see the difference below:

**Javascript:**
```
const name = "Duski";
const born = "12-12-1995";
//
let data = name+' was born '+born;
console.log(data);
```
The example above combines variables and strings ```name+' was born '+born;``` using javascript. we will see what the difference is by using syntax string literals. Certainly not a problem if the variables we combine are still few. but it will be complicated if we have to combine it with ```+ " " +```. Therefore we will make it easier by utilizing the string literal syntax that is in javascript es6.

**Using string literal:**

We will use string literals and will see how the differences are.
``` javascript
const name = "Duski";
const born = "12-12-1995";
//
let data = `My name is ${name}, I was born ${born}`;
console.log(data);
```
- If in javascript we use ```" "``` to wrap strings and variables, in javascript es6 we will wrap the string and variable with ``` ` ` ```. 

- If in javascript we separate variables and strings with ```+ var +```, then using string literals we don't need to separate them, but we will add them in this ```${var}```. We can see the results below by doing ```console.log ()```.

![Screenshot_10.png](https://ipfs.busy.org/ipfs/QmehUwyBqq1NHHS3sSst8wwVbAb9D3RGQumBMsYGt4yun1)
<br>
**Using string literal work with DOM:**

*String literals* can also be used for those of you who want to write a ***DOM element*** in the type of a String.
**Example:**
``` javascript
const name = "Duski";
const born = "12-12-1995";
//
let element = `
<div>
	<h1>My name is ${name}</h1>
	<h2>I was bor ${born}</h2>
</div>
`;
console.log(element);
```
We can print out from the Html element which we combine with the variable, of course this is very helpful for those of you who often play with **HTML elements**.We will see the results as shown below:

![Screenshot_11.png](https://ipfs.busy.org/ipfs/QmPy3zucfDefZvhHBtFvPHcqyAYbZ8LKbSYrvJKAZk4riX)
<br>

**Use string literal as a function**

we can also use *string literals* as functions. We can see an example like the following
``` javascript
const name = "Duski"
func = (string,variable)=>{
 console.log(string);
}
let data = func`My name is ${name}`
```
- We can make string literal functions like this ```func`My name is ${name}```` . This is the same when we make a function like this ```func(My name is,${name})```, If we look at this it means that this function has two parameters, separating strings and variables.
- Then we can run the string like this ```func = (string,variable)=>{}```, **string parameter is the string variable** ```"My name is"``` and **the variable parameter is the variable name** ```${name}```. to see the results we can see in the picture below.

![Screenshot_13.png](https://ipfs.busy.org/ipfs/QmcfmCgJpenLixBBmPB5GsocoTgbCvkkVZJ8BtMaKLRAGS)

- If we compile the index.js file through ```npm run build``` which we have set in the previous [tutorial](https://steemit.com/utopian-io/@duski.harahap/learn-es6-part-1-intro-setup-babel-and-understand-var-let-const) then we will get the compile results as follows.

![Screenshot_12.png](https://ipfs.busy.org/ipfs/QmSopqMkg9PzeDwSmqskeKBr3SAZjhzovdR8LcNYLp5UEt)


### Shorthand and Destructuring
<br>
- **Shorthand**
on es6, we can shorten the writing of keys and values on an object. but the key and value requirements must have the same name. for more details, we can see in the following example.

**Shorthand in object:**
``` javascript
const name = "Millea";
const lastname = "Duski"
// 
let obj = {
	name, // it means name = name
	lastname
}
console.log(obj)
```
We can see picture above, if the key pair and the value of an object are named the same, we don't need to write ```name : name``` but just need ```name```.and if you do ```console.log ()``` we will see the object results from the variable ```let obj```.

![Screenshot_14.png](https://ipfs.busy.org/ipfs/QmRAzrCNu7xJ6p7uJnJ4zSeuYnb3jBsu3fFYgzoRnRiaSr)
<br>
**Shorthand in function:**
``` javascript
//shorthand in function
const name = "Millea";
const lastname = "Duski"
//
let funcData = function getData(){
	return `My name is ${name} ${lastname}`
}
let obj = {
	funcData
}
//
console.log(obj.funcData())
```
not only on string objects or integers but you also use shorthand in the function too. We can save the function in a variable ```let funcData = function getData(){}```that we enter in the object. then not much different from before we only need to write down the variable where to store the function.

![Screenshot_15.png](https://ipfs.busy.org/ipfs/QmY9DKmNbfyArnAiDRYhRwBakfrHBNCybfg7KjD2LSmGzb)
<br>
- **Destructuring**

There are unique things that we can do on es6, we can separate an object or array into a new variable. for more details we can see below:

**Pass the parameter:**
``` javascript
//Destructuring
let obj = {
	name : 'Millea',
	lastname : 'Duski'
}
const {lastname, name} = obj
//
console.log(name)
```
- We can pass the key object with ```{}```, then we define which object we will use ```{lastname, name} = obj```.

- The object that is passed on ```{lastname, name}``` is the key of the object, So the sequence of objects that are not operated need to be in accordance with the order of the keys that exist on the object.

**Assign value:**

We can also assign new values ​to the parameters that are passed in the object above. the following is an example:
``` javascript
let obj = {
	name : 'Millea',
	lastname : 'Duski'
}
//assign new value
const {lastname : myLastName, name : newName} = obj
//
console.log(myLastName,newName)
```
let's guess what will happen. We assign a new value that is ```myLastname```. We can assign new values ​​with ```lastname : myLastName```. If we console.log () we will see the results as follows.

![ezgif.com-video-to-gif (1).gif](https://ipfs.busy.org/ipfs/QmdMnnGetznWtUhVRHx4EnFUATxtdeTCybr7VAk4aHEPyh)

We have finished learning about ***string literals, shorthand and Destructuring***. there are still many syntaxes of es6 that we will discuss. you can explore more. Hopefully, this tutorial can help you who want to learn ES6. thank you.

#### Curriculum
[Intro Setup babel and understand var,let,const](https://steemit.com/utopian-io/@duski.harahap/learn-es6-part-1-intro-setup-babel-and-understand-var-let-const)
[Arrow Function, Default parameter, rest and spread](https://steemit.com/utopian-io/@duski.harahap/learn-es6-part-2-arrow-function-default-parameter-and-rest-spread)

#### Proof of work done
https://github.com/milleaduski/learnEs6
πŸ‘  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 30 others
properties (23)
authorduski.harahap
permlinklearn-es6-part-3-string-literal-shorthand-destructuring-object
categoryutopian-io
json_metadata{"community":"busy","app":"busy/2.5.5","format":"markdown","tags":["utopian-io","tutorials","javascript","es6"],"users":["duski.harahap"],"links":["https://github.com/babel/babel","https://github.com/babel/babe","https://steemit.com/utopian-io/@duski.harahap/learn-es6-part-1-intro-setup-babel-and-understand-var-let-const","https://steemit.com/utopian-io/@duski.harahap/learn-es6-part-1-intro-setup-babel-and-understand-var-let-const","https://steemit.com/utopian-io/@duski.harahap/learn-es6-part-2-arrow-function-default-parameter-and-rest-spread","https://github.com/milleaduski/learnEs6"],"image":["https://ipfs.busy.org/ipfs/QmehUwyBqq1NHHS3sSst8wwVbAb9D3RGQumBMsYGt4yun1","https://ipfs.busy.org/ipfs/QmPy3zucfDefZvhHBtFvPHcqyAYbZ8LKbSYrvJKAZk4riX","https://ipfs.busy.org/ipfs/QmcfmCgJpenLixBBmPB5GsocoTgbCvkkVZJ8BtMaKLRAGS","https://ipfs.busy.org/ipfs/QmSopqMkg9PzeDwSmqskeKBr3SAZjhzovdR8LcNYLp5UEt","https://ipfs.busy.org/ipfs/QmRAzrCNu7xJ6p7uJnJ4zSeuYnb3jBsu3fFYgzoRnRiaSr","https://ipfs.busy.org/ipfs/QmY9DKmNbfyArnAiDRYhRwBakfrHBNCybfg7KjD2LSmGzb","https://ipfs.busy.org/ipfs/QmdMnnGetznWtUhVRHx4EnFUATxtdeTCybr7VAk4aHEPyh"]}
created2018-08-16 19:50:18
last_update2018-08-16 19:50:18
depth0
children4
last_payout2018-08-23 19:50:18
cashout_time1969-12-31 23:59:59
total_payout_value16.476 HBD
curator_payout_value5.173 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length7,782
author_reputation60,094,717,098,672
root_title"Learn Es6 part#3 String literal, Shorthand, Destructuring object"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id68,423,401
net_rshares16,621,261,369,435
author_curate_reward""
vote details (94)
@portugalcoin ·
$7.99
Thank you for your contribution.

- Nice work on the explanations of your code, although adding a bit more comments to the code can be helpful as well.
- The tutorial is very basic but interesting for beginners.

Your contribution has been evaluated according to [Utopian policies and guidelines](https://join.utopian.io/guidelines), as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, [click here](https://review.utopian.io/result/8/31311424).

---- 
Need help? Write a ticket on https://support.utopian.io/. 
Chat with us on [Discord](https://discord.gg/uTyJkNm). 
[[utopian-moderator]](https://join.utopian.io/)
πŸ‘  , , , , , ,
properties (23)
authorportugalcoin
permlinkre-duskiharahap-learn-es6-part-3-string-literal-shorthand-destructuring-object-20180816t204458642z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"links":["https://join.utopian.io/guidelines","https://review.utopian.io/result/8/31311424","https://support.utopian.io/","https://discord.gg/uTyJkNm","https://join.utopian.io/"],"app":"steemit/0.1"}
created2018-08-16 20:44:57
last_update2018-08-16 20:44:57
depth1
children1
last_payout2018-08-23 20:44:57
cashout_time1969-12-31 23:59:59
total_payout_value6.027 HBD
curator_payout_value1.963 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length703
author_reputation598,828,312,571,988
root_title"Learn Es6 part#3 String literal, Shorthand, Destructuring object"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id68,427,146
net_rshares6,138,528,366,612
author_curate_reward""
vote details (7)
@utopian-io ·
$0.02
Thank you for your review, @portugalcoin!

So far this week you've reviewed 25 contributions. Keep up the good work!
πŸ‘  ,
properties (23)
authorutopian-io
permlinkre-re-duskiharahap-learn-es6-part-3-string-literal-shorthand-destructuring-object-20180816t204458642z-20180818t231009z
categoryutopian-io
json_metadata"{"app": "beem/0.19.42"}"
created2018-08-18 23:10:09
last_update2018-08-18 23:10:09
depth2
children0
last_payout2018-08-25 23:10:09
cashout_time1969-12-31 23:59:59
total_payout_value0.019 HBD
curator_payout_value0.004 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length116
author_reputation152,955,367,999,756
root_title"Learn Es6 part#3 String literal, Shorthand, Destructuring object"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id68,644,184
net_rshares18,067,020,297
author_curate_reward""
vote details (2)
@steem-ua ·
Hi @duski.harahap! We are @steem-ua, a new Steem dApp, computing UserAuthority for all accounts on Steem. We are currently in test modus upvoting quality Utopian-io contributions! Nice work!
properties (22)
authorsteem-ua
permlinkre-learn-es6-part-3-string-literal-shorthand-destructuring-object-20180816t205852z
categoryutopian-io
json_metadata"{"app": "beem/0.19.54"}"
created2018-08-16 20:58:51
last_update2018-08-16 20:58:51
depth1
children0
last_payout2018-08-23 20:58:51
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length190
author_reputation23,214,230,978,060
root_title"Learn Es6 part#3 String literal, Shorthand, Destructuring object"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id68,428,067
net_rshares0
@utopian-io ·
$0.02
Hey @duski.harahap
 **Thanks for contributing on Utopian**.
We’re already looking forward to your next contribution!

**Want to chat? Join us on Discord https://discord.gg/h52nFrV.**

<a href='https://v2.steemconnect.com/sign/account-witness-vote?witness=utopian-io&approve=1'>Vote for Utopian Witness!</a>
πŸ‘  ,
properties (23)
authorutopian-io
permlinkre-learn-es6-part-3-string-literal-shorthand-destructuring-object-20180819t095008z
categoryutopian-io
json_metadata"{"app": "beem/0.19.42"}"
created2018-08-19 09:50:09
last_update2018-08-19 09:50:09
depth1
children0
last_payout2018-08-26 09:50:09
cashout_time1969-12-31 23:59:59
total_payout_value0.019 HBD
curator_payout_value0.004 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length306
author_reputation152,955,367,999,756
root_title"Learn Es6 part#3 String literal, Shorthand, Destructuring object"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id68,682,001
net_rshares18,070,046,292
author_curate_reward""
vote details (2)