Welcome back to our JavaScript tutorial for beginners.
In this video, Juan shares his top three features of JavaScript in the new ES6 specification. If you’re new to the language or the new specification, these three features will be helpful to make your code more readable and concise.
1. Destructuring Assignment
Destructuring is a convenient way to extract elements from arrays or attributes from objects. It can make your code more readable and concise. Let’s see an example:
const arr = [1, 2, 3, 4, 5];
const [item1, item2, ...rest] = arr;
console.log(item1, item2, rest); // 1 2 [3, 4, 5]
Here, we use the destructuring syntax to extract the first two items of the array and the rest of the items in a new array. The same syntax can be used to extract properties from an object:
const person = {
name: 'John',
age: 30,
city: 'New York',
};
const { name, age, ...other } = person;
console.log(name, age, other); // John 30 { city: 'New York' }
2. Default Parameters
We can specify default parameters to our functions so that we can have default values for them. This is a pretty convenient way, especially when using TypeScript. Let’s see an example:
function greet(name = 'Juan', message = 'Welcome') {
console.log(`${message} ${name}`);
}
greet(); // Welcome Juan
greet('John', 'Hello'); // Hello John
Here, we use the default parameter syntax to set the default values for name and message. If we don't pass any arguments to the function, it will use the default values.
3. Template Literals
Template literals provide an easy way to create strings with embedded expressions. It just makes it easier to concatenate data. Let’s see an example:
const name = 'John';
const message = `Welcome ${name}`;
console.log(message); // Welcome John
Here, we use the template literal syntax to create a string with an embedded expression. This is a more concise way to concatenate strings than using the + operator.
In conclusion, these are the three top features of JavaScript in the new ES6 specification. They are simple to use and can speed up your coding. Juan recommends using these features in your everyday coding. Thank you!
Top comments (0)