New Features for Strings, Objects, and Arrays

Learn practical ES6 features for strings, objects, and arrays, along with common usage patterns.

🎯 Introduction

This article covers template literals, string methods, object destructuring, shorthand properties, array spread syntax, and new array methods. These features help you write concise, readable, capable frontend code and work more efficiently.


✨ New String Features

ES6 improves string handling, most notably with template literals. They make it easier to insert variables and write multiline text.

Previously, string concatenation often required repeated + operators:

const name = 'Alex';
const age = 18;
const str = 'My name is ' + name + ', and I am ' + age + ' years old.';
console.log(str);

ES6 template literals, enclosed in backticks (`), make this easier:

const name = 'Alex';
const age = 18;
const str = `My name is ${name}, and I am ${age} years old.`;
console.log(str);

Use ${} to insert variables directly into the string.

ES6 also adds several useful string methods:

  • startsWith: Checks whether a string starts with a given substring:
    const str = 'hello world';
    console.log(str.startsWith('hell')); // true
    
  • endsWith: Checks whether a string ends with a given substring:
    console.log(str.endsWith('world')); // true
    
  • includes: Checks whether a string contains a given substring:
    console.log(str.includes('lo wo')); // true
    
  • repeat: Repeats a string:
    console.log('ha'.repeat(3)); // "hahaha"
    
These methods are useful for validation and simple searches, without complicated index checks.

🧩 New Object Features

Objects are a core data structure in frontend development. ES6 introduces several useful improvements for working with them.

Shorthand Properties

Previously, you had to specify both the property name and its value:

const name = 'Emma';
const age = 20;
const obj = {
    name: name,
    age: age,
};

With ES6, use shorthand when the property name matches the variable name:

const name = 'Emma';
const age = 20;
const obj = { name, age };

This reduces repetition and opportunities for mistakes.

Object Destructuring Assignment

Destructuring is a powerful ES6 feature for extracting properties from objects:

const person = { name: 'Emma', age: 20 };
const { name, age } = person;
console.log(name, age); // Emma 20

You can also provide default values for destructured variables:

const { name, age, gender = 'female' } = person;
console.log(gender); // female

A New Method: Object.assign

Object.assign merges properties from source objects into a target object:

const obj1 = { a: 1 };
const obj2 = { b: 2 };
const obj3 = Object.assign({}, obj1, obj2);
console.log(obj3); // { a: 1, b: 2 }

Use it to make shallow copies or merge configuration options.

Object destructuring is also useful for function parameters: extract properties directly from an object parameter to make the code clearer.

🔢 New Array Features

Arrays are widely used in frontend development, and ES6 adds several convenient ways to work with them.

Spread Syntax

Spread syntax uses three dots, ..., to expand an array:

const arr1 = [1, 2];
const arr2 = [3, 4];
const arr3 = [...arr1, ...arr2];
console.log(arr3); // [1, 2, 3, 4]

This is more concise than calling concat.

Objects also support spread syntax!

Array Destructuring Assignment

Like objects, arrays support destructuring assignment:

const arr = [100, 200, 300];
const [a, b, c] = arr;
console.log(a, b, c); // 100 200 300

New Array Methods

Useful array methods include:

  • find: Returns the first element that satisfies a condition.
    const arr = [5, 12, 8, 130, 44];
    const found = arr.find(function (value) {
        return value > 10;
    });
    console.log(found); // 12
    
  • findIndex: Returns the index of the first element that satisfies a condition.
    const index = arr.findIndex(function (value) {
        return value > 10;
    });
    console.log(index); // 1
    
  • includes: Checks whether an array contains a value.
    console.log(arr.includes(8)); // true
    
  • fill: Fills an array with a value.
    const arr2 = new Array(4).fill(1);
    console.log(arr2); // [1, 1, 1, 1]
    
fill modifies the original array. Take care to avoid unintended changes.

❓ Knowledge Check

Q1: How do template literals differ from regular strings?

Template literals support embedded variables and expressions, concise interpolation, and multiline strings.

Q2: Does Object.assign create a deep copy or a shallow copy?

Object.assign creates a shallow copy. It copies only top-level properties; nested objects remain references.

Q3: Can array destructuring skip elements?

Yes. For example, [a, , c] = [1, 2, 3] assigns 1 to a and 3 to c, skipping the middle element.


🧪 Exercises:

  1. Use a template literal to introduce yourself, including your name, age, and hobby.
const name = 'Alex';
const age = 25;
const hobby = 'basketball';
// Write an introduction using a template literal here
const intro = ``;
console.log(intro);
  1. Use object destructuring and spread syntax to merge two arrays, then use find to locate the first number greater than 50 in the merged array.
const arr1 = [10, 20, 60];
const arr2 = [30, 50, 70];
// Merge the arrays
const merged = [];
// Find the first number greater than 50
const found = null;

console.log(merged);
console.log(found);

🎉 You now understand template literals, object destructuring, and array spread syntax! Keep practicing to become more comfortable with these everyday frontend tools.