Intro to ES6 and its features
JavaScript language specification ECMAScript 2015, also known as ES6, is the latest major update to the JavaScript language specification.It introduced many new features to the language, including let and const variables, arrow functions, template literals, classes, modules, and more. In this blog post, we will explore some of the most significant features of ES6 and provide code examples for each of them.
- let and const variables
Using the var keyword prior to ES6 allows JavaScript variables to be declared. ES6 introduced two new ways of declaring variables: let and const. ES characteristics differ between var and let/const in that var variables are function-scoped, whereas let and const variables are block-scoped.Block-scoped variables are only accessible within the block they are declared in. The const keyword is used to declare a constant variable, which cannot be reassigned once it has been initialized. Reassigning let variables, however, is possible. Here’s an example:
// var example function varExample() { var x = 1; if (true) { var x = 2; // same variable } console.log(x); // 2 } varExample(); // let example function letExample() { let x = 1; if (true) { let x = 2; // different variable } console.log(x); // 1 } letExample(); // const example const PI = 3.14159; console.log(PI); // 3.14159 PI = 3; // Error: Assignment to constant variable
- Arrow functions
Arrow functions are a shorthand way of defining functions in JavaScript. They are more concise than regular functions and have a more predictable behavior when it comes to the ‘this’ keyword. Here’s an example:
// Regular function function regularFunction(x, y) { return x + y; } // Arrow function const arrowFunction = (x, y) => x + y; console.log(regularFunction(2, 3)); // 5 console.log(arrowFunction(2, 3)); // 5
- Template literals
Template literals provide a way to embed expressions inside string literals in JavaScript. They use backticks (`) instead of single or double quotes and allow for string interpolation. Here’s an example:
const name = 'John'; const age = 30; const job = 'Web Developer'; const message = `My name is ${name}, I'm ${age} years old, and I work as a ${job}.`; console.log(message); // My name is John, I'm 30 years old, and I work as a Web Developer.
- Classes
ES6 introduced a class syntax for creating objects that behave like traditional classes in other programming languages. Classes in JavaScript are just syntactic sugar over the existing prototype-based inheritance model. Here’s an example:
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
sayHello() {
console.log(`Hello, my name is ${this.name}.`);
}
}
const john = new Person('John', 30);
john.sayHello(); // Hello, my name is John.
- Modules
ES6 introduced a new module syntax for organizing code into reusable, independent units. It is possible to import and export modules in their own files. Here’s an example
// math.js
Conclusion
The ES6 feature in JScript has made numerous impacts in the world of development and developers alike. Concise writing principles along with precision has allowed to save developers time and let them focus on greater issues. For better understanding of ES6 features with example code refer to https://geekalgo.com/ or https://es6.io.