4. Modern ES6+ Features & Modular JavaScript

Optional Chaining & Nullish Coalescing

Avoid "Cannot read properties of undefined" errors when parsing deeply nested test configs or API responses:
const response = {
  data: {
    user: {
      profile: { name: "Alice" }
    }
  }
};

// Safe access with ?.:
const avatarUrl = response?.data?.user?.profile?.avatar ?? "default-avatar.png";

// Nullish coalescing (??) only replaces null or undefined (not 0 or false):
const timeout = customConfig.timeout ?? 30000;

ES Modules (import / export)

// utils/locatorHelpers.js
export const getRowSelector = (id) => tr[data-id="${id}"];

// In test file:
import { getRowSelector } from "../utils/locatorHelpers.js";

Task

  • Implement getSafeTimeout:
  • 1. Accept config object.
  • 2. Return config?.timeouts?.action ?? 5000.
πŸ’‘Common doubts
Did you like the problem?
Lesson 4 of 35
JavaScript
Loading code editor…
Next