Back to JavaScript
2026-01-305 min read

SyntaxError: functions cannot be labelled (JavaScript)

Learn SyntaxError: functions cannot be labelled (JavaScript) step by step with clear examples and exercises.

Title: A full guide on SyntaxError: Functions Cannot Be Labelled (JavaScript)

Why This Matters

Understanding JavaScript's syntax rules, including the prohibition of labelling functions, is vital for writing clean and error-free code. This knowledge can help you avoid common pitfalls during development, ensuring your applications run smoothly and are less prone to bugs. Additionally, being aware of such restrictions is essential when preparing for interviews or real-world coding challenges.

JavaScript's syntax rules provide a consistent structure that makes the language easier to learn, understand, and maintain. By avoiding labelled functions, JavaScript ensures that functions behave consistently throughout the language, which can help prevent unintended consequences and reduce the likelihood of errors.

Prerequisites

To fully grasp this lesson, you should have a basic understanding of:

  1. JavaScript syntax and semantics
  2. Function declarations and expressions
  3. Variables and scoping rules in JavaScript
  4. Understanding the difference between strict mode (use strict) and non-strict mode
  5. Basic knowledge of loops, conditional statements, and error handling constructs in JavaScript
  6. Familiarity with common JavaScript data types such as numbers, strings, arrays, objects, and booleans.
  7. Understanding the concept of hoisting in JavaScript.
  8. Knowledge of variable scopes (global, local, block) and their behavior.
  9. Comprehension of the var, let, and const keywords in JavaScript.

Core Concept

In non-strict mode (i.e., without the use strict directive), JavaScript allows functions to be declared at the top level, inside a block, or as the body of an if statement. However, it forbids labelling these functions. This means you cannot assign a label before a function declaration:

label1:
function myFunction() { /* ... */ } // SyntaxError: functions cannot be labelled

In strict mode (enabled with use strict), the rules are more stringent. Functions can only be declared at the top level or inside a block, and they still cannot be labelled:

"use strict";
label1:
function myFunction() { /* ... */ } // SyntaxError: functions cannot be labelled

Why This Rule Exists

This rule helps maintain the consistency of JavaScript's syntax and avoid potential confusion. Labelling a function could create ambiguity regarding its scope or behavior, as labels are typically used to define loops or error handling constructs in other programming languages. By preventing labelled functions, JavaScript ensures that functions behave consistently throughout the language.

Function Labels vs Variable Names

It's essential to understand the distinction between function labels and variable names in JavaScript. Function labels are used to identify a specific point in the code where a break, continue, or return statement can be applied, while variable names store data values that can be accessed throughout the program.

Understanding Function Declarations and Expressions

Function declarations are hoisted to the top of their containing scope, whereas function expressions must be assigned to a variable before they can be invoked. Here's an example of both:

// Function declaration (hoisted)
function myFunctionDeclaration() {
console.log("Hello from myFunctionDeclaration!");
}

myFunctionDeclaration(); // Output: "Hello from myFunctionDeclaration!"

// Function expression (not hoisted)
const myFunctionExpression = function () {
console.log("Hello from myFunctionExpression!");
};

myFunctionExpression(); // Output: "Hello from myFunctionExpression!"

Worked Example

Let's consider an example where we attempt to label a function and encounter a SyntaxError:

label1:
function myFunction() {
console.log("Hello from myFunction!");
}

// SyntaxError: functions cannot be labelled

To resolve this error, simply remove the label before the function declaration:

function myFunction() {
console.log("Hello from myFunction!");
}

Common Mistakes

  1. Attempting to label a function: As discussed earlier, labelling functions is not allowed in JavaScript.
  2. Confusing labels with variable names: Labels and variable names serve different purposes in the language. Be mindful of their distinct roles.
  3. Ignoring error messages: When encountering a SyntaxError related to labelled functions, it's essential to understand the cause and correct the issue accordingly.
  4. Attempting to label a function inside a loop or conditional statement: In strict mode, functions can only be declared at the top level or inside a block, so attempting to label a function within a loop or conditional statement will result in a SyntaxError.
  5. Misunderstanding hoisting and its implications on function declarations.
  6. Using var instead of let or const, leading to unintended global variable scope issues.
  7. Not understanding the difference between function expressions and declarations, and their respective hoisting behavior.

Practice Questions

  1. Rewrite the following code snippet to remove the label before the function declaration:
label1:
function myFunction() {
console.log("Hello from myFunction!");
}
  1. What is the purpose of labelling functions in JavaScript, and why can't we do it?
  2. Explain the difference between function labels and variable names in JavaScript.
  3. Why are labelled functions not allowed within loops or conditional statements in strict mode?
  4. Describe the hoisting behavior of function declarations in JavaScript.
  5. What is the purpose of using let and const instead of var, and how do they affect variable scopes in JavaScript?
  6. Write a JavaScript program that demonstrates the difference between a function declaration and a function expression, including their respective hoisting behavior.

FAQ

  1. Why can't I label a function in JavaScript?
  • Labelling functions in JavaScript could create ambiguity regarding their scope or behavior. To maintain consistency, JavaScript forbids labelled functions.
  1. What are the consequences of attempting to label a function in JavaScript?
  • Attempting to label a function will result in a SyntaxError. It's essential to remove the label before the function declaration to resolve this issue.
  1. Can I use labels for other purposes in JavaScript, such as defining loops or error handling constructs?
  • Yes, labels can be used for these purposes in JavaScript. However, they should not be confused with variable names or used to label functions.
  1. Why are labelled functions not allowed within loops or conditional statements in strict mode?
  • In strict mode, functions can only be declared at the top level or inside a block. Attempting to label a function within a loop or conditional statement would violate this rule and result in a SyntaxError.
  1. How does hoisting affect function declarations in JavaScript?
  • Function declarations are hoisted to the top of their containing scope, meaning they can be called before they are declared without causing an error. However, their actual assignment occurs at the point where they are declared.
  1. What is the difference between let, const, and var in JavaScript?
  • let and const are block scoped variables that provide better control over variable lifetimes and help prevent unintended global variable scope issues. In contrast, var is function scoped but can lead to such issues due to its hoisting behavior.
  1. Write a JavaScript program that demonstrates the difference between a function declaration and a function expression, including their respective hoisting behavior.
// Function Declaration (hoisted)
function myFunctionDeclaration() {
console.log("Hello from myFunctionDeclaration!");
}

console.log(myFunctionDeclaration); // Outputs: function myFunctionDeclaration() { ... }
myFunctionDeclaration(); // Output: "Hello from myFunctionDeclaration!"

// Function Expression (not hoisted)
const myFunctionExpression = function () {
console.log("Hello from myFunctionExpression!");
};

console.log(myFunctionExpression); // Outputs: undefined
myFunctionExpression(); // Output: "Hello from myFunctionExpression!" after the assignment to `myFunctionExpression`
SyntaxError: functions cannot be labelled (JavaScript) | JavaScript | XQA Learn