Demo 1
Create new Javascript file named demo1.js. This file contains code demo as below:
let [firstName, middleName, lastName] = ['John', 'Thomas', 'Smith'];
console.log('First Name: ' + firstName);
console.log('Middle Name: ' + middleName);
console.log('Last Name: ' + lastName);
Run Demo 1
Use node demo1.js statement run code demo
First Name: John
Middle Name: Thomas
Last Name: Smith
Demo 2
Create new Javascript file named demo2.js. This file contains code demo as below:
let [, , lastName] = ['John', 'Thomas', 'Smith'];
console.log('Last Name: ' + lastName);
Run Demo 2
Use node demo2.js statement run code demo
Last Name: Smith
Demo 3
Create new Javascript file named demo3.js. This file contains code demo as below:
let firstName, middleName, lastName;
[firstName, middleName, lastName] = ['John', 'Thomas', 'Smith'];
console.log('First Name: ' + firstName);
console.log('Middle Name: ' + middleName);
console.log('Last Name: ' + lastName);
Run Demo 3
Use node demo3.js statement run code demo
First Name: John
Middle Name: Thomas
Last Name: Smith
Demo 4
Create new Javascript file named demo4.js. This file contains code demo as below:
let [firstName, ...rest] = ['John', 'Thomas', 'Smith'];
console.log('First Name: ' + firstName);
console.log('Rest: ' + rest);
Run Demo 4
Use node demo4.js statement run code demo
First Name: John
Rest: Thomas,Smith
Demo 5
Create new Javascript file named demo5.js. This file contains code demo as below:
let [name = "Guest", surname = "Anonymous"] = ["Julius"];
console.log('Name: ' + name);
console.log('Surname: ' + surname);
Run Demo 5
Use node demo5.js statement run code demo
Name: Julius
Surname: Anonymous