Back to cheat sheet hub

JavaScript Cheat Sheet

A practical JavaScript revision page for our automation curriculum: the exact concepts students need before and during Playwright.

Variables and scope Arrays and objects Async JavaScript

Why this matters

Automation engineers write test code, helper functions, data builders, parsers, and API utilities in JavaScript every day.

Use it for

Live classes, interview prep, debugging code, and quickly recalling syntax during labs.

Focus

Concepts that directly help in Playwright and Node-based automation projects.

Style

Prefer clear naming, pure helpers where possible, and small reusable functions.

Variables and Types

Use const by default, let when reassignment is required, and avoid var.

const course = 'Playwright';
let students = 120;
const isLive = true;
const topics = ['arrays', 'functions', 'async'];
const profile = { name: 'Pramod', role: 'mentor' };

Functions

Functions are the core building block for reusable automation utilities and data transformations.

function formatScore(score) {
  return `${score}%`;
}

const addPoints = (current, extra) => current + extra;

function buildStudent(name, batch = 'LPW') {
  return { name, batch };
}

Conditionals and Loops

Use readable conditionals and prefer array methods over deeply nested manual loops when possible.

if (score >= 80) {
  console.log('Strong result');
} else {
  console.log('Needs improvement');
}

for (const topic of topics) {
  console.log(topic);
}

Arrays and Array Methods

These methods are everywhere in test data prep, result parsing, and report shaping.

const scores = [45, 72, 88, 95];

const passed = scores.filter(score => score >= 50);
const boosted = scores.map(score => score + 5);
const total = scores.reduce((sum, score) => sum + score, 0);
const topScore = Math.max(...scores);

Objects and Destructuring

Objects model API payloads, test fixtures, and reusable configuration.

const student = {
  name: 'Lucky',
  github: 'luckydutta96',
  streak: 7,
};

const { name, streak } = student;
const updated = { ...student, points: 120 };

Closures

Closures help when functions need to remember values between calls.

function createCounter() {
  let count = 0;

  return function increment() {
    count += 1;
    return count;
  };
}

const next = createCounter();

Promises and Async/Await

Modern automation code relies on async flows for browser actions, API calls, and file operations.

async function getLeaderboard() {
  const response = await fetch('/api/leaderboard');
  const data = await response.json();
  return data;
}

const results = await Promise.all([
  getLeaderboard(),
  getLeaderboard(),
]);

Error Handling

Always wrap risky async operations when the failure needs a clean fallback or better logs.

try {
  const data = await getLeaderboard();
  console.log(data);
} catch (error) {
  console.error('Failed to fetch leaderboard', error);
}

Modules

Split utilities and page helpers into focused files and export only what is needed.

// math.js
export function add(a, b) {
  return a + b;
}

// main.js
import { add } from './math.js';
console.log(add(2, 3));