#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
JavaScript Fundamentals

Keywords in JavaScript

Keywords are words that have a special meaning in JavaScript's grammar, such as if, function and return. Because the language uses them to understand your code, most of them are reserved: you cannot use them as names for variables, functions or classes. Knowing the list helps you read code and avoid confusing syntax errors.

Keywords grouped by purpose

PurposeKeywords
Declaringvar, let, const, function, class
Decisionsif, else, switch, case, default
Loops and jumpsfor, while, do, break, continue, return
Errorstry, catch, finally, throw
Objects and classesnew, this, super, extends, instanceof, delete, in
Modulesimport, export
Values and operatorstrue, false, null, typeof, void
Async and generatorsasync, await, yield
Otherdebugger, with (forbidden in strict mode)

Strictly reserved, reserved later, and contextual

Not every keyword is reserved in the same way. Words like if, class and return are always reserved. Some words, including let, static, implements, interface, package, private, protected and public, are reserved only in strict mode, which is on automatically inside classes and ES modules. enum is reserved for possible future use.

A few words are contextual: they act as keywords only in certain positions. of matters in for...of, get and set inside objects and classes, and async before a function. await is a keyword inside async functions and at the top level of modules. Outside those places, these words could technically be used as names, though doing so is confusing.

Keywords in action

"use strict";

class Account extends Object {
  static count = 0;

  constructor(owner) {
    super();
    this.owner = owner;
    Account.count++;
  }
}

async function loadAccount(id) {
  try {
    const response = await fetch(`/api/accounts/${id}`);
    if (!response.ok) throw new Error("Not found");
    return await response.json();
  } catch (err) {
    console.error(err.message);
    return null;
  }
}

for (const key of ["owner", "balance"]) {
  console.log(key, typeof key);   // string
}

// Reserved words ARE allowed as property names
const style = { class: "card", default: true };
console.log(style.class);

Mistakes caused by reserved words

Trying to name a variable after a keyword, such as let new = 5; or const class = "a";, gives "Unexpected token" or "Unexpected reserved word" errors that do not obviously point to the name. If an otherwise correct line fails, check whether a name is reserved. Choose alternatives like newItem or className.

Code that worked in a plain script can also break when moved into a module or class, because strict mode reserves extra words. A variable called package or private is a typical example.

Note that true, false and null are literals that cannot be redefined, but undefined, NaN and Infinity are actually properties of the global object rather than keywords. They cannot be reassigned globally, yet undefined can still be shadowed by a local variable, which is one reason linters warn about it.

A handy habit

Modern editors colour keywords differently from ordinary names. If a variable name suddenly appears in keyword colour, rename it before you run the code.

Takeaway: keywords define JavaScript's grammar, most cannot be used as identifiers, strict mode reserves a few more, contextual words like of and await only matter in certain places, and reserved words remain fine as object property names.

Ready to master real-world web development?

Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.

Explore Course