Master ES6 Basics: let/const, Template Strings, Set, Map, and Destructuring
This article introduces four essential ES6 features—let/const variable declarations, template strings, the new Set and Map collections, and enhanced function parameters with defaults and object destructuring—providing clear code examples that demonstrate their advantages over ES5 syntax.
Using let and const to declare variables
ES5 lacks block scope and constant declarations. ES6 introduces let and const to solve these problems.
Example with var (ES5):
var a = 1;
if (true) {
var b = 2;
}
console.log(a); // 1
console.log(b); // 2Example with let (ES6):
let a = 1;
if (true) {
let b = 2;
}
console.log(a); // 1
console.log(b); // ReferenceError: b is not definedUsing let in loops prevents the classic closure issue:
var a = [];
for (var i = 0; i < 5; i++) {
a.push(function() { console.log(i); });
}
a.forEach(function(v) { v(); }); // 5,5,5,5,5 let b = [];
for (let i = 0; i < 5; i++) {
b.push(function() { console.log(i); });
}
b.forEach(function(v) { v(); }); // 0,1,2,3,4Defining constants with const:
// ES5
var MY_CONSTANT = true;
MY_CONSTANT = false; // allowed but unsafe
// ES6
const MY_CONSTANT = true;
MY_CONSTANT = false; // TypeError: Assignment to constant variableTemplate strings
String concatenation becomes concise with back‑ticks and interpolation.
// ES5
var url = 'http://www.' + domain + '.com/' + path + '?' + queryParams;
// ES6
let url = `http://www.${domain}.com/${path}?${queryParams}`;New Set and Map objects
Setautomatically removes duplicate values.
let collection = new Set();
collection.add(1);
collection.add(2);
collection.add(1);
console.log(collection); // Set {1, 2} Mapstores key‑value pairs with any type of key.
let collection = new Map();
collection.set('a', 'abc');
collection.set('b', 'xyz');
collection.forEach(function(value, key) {
console.log(key + ':' + value);
});
console.log(collection.size); // 2Function parameters
ES6 allows default values directly in the parameter list.
function doSomething(someObject = {}) {
console.log(someObject);
}Object destructuring in parameters
Extract properties from an object argument in one step.
function doSomething({ propOne, propTwo, propThree }) {
console.log(propOne);
console.log(propTwo);
console.log(propThree);
}Reference: https://www.codementor.io/javascript/tutorial/4-easy-ways-to-start-using-es2015
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java High-Performance Architecture
Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
