Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

Misplacing Semicolon

Due to a misplaced semicolon, this code block will execute regardless of the value of x.

if (x == 19);
{
  // code block 
}

Breaking a Return Statement

JavaScript automatically closes a statement at the end of a line by default.

As a result, these two examples will produce the same outcome:

Example 1

function myFunction(a) {
  let power = 10 
  return a * power
}

Example 2

function myFunction(a) {
  let power = 10;
  return a * power;
}

JavaScript also allows you to split a statement across two lines.

Therefore, example 3 will produce the same result as well.

Example 3

function myFunction(a) {
  let
  power = 10
  return a * power;
}

But what happens if you split the return statement across two lines like this:

Example 4

function myFunction(a) {
  let
  power = 10
  return
  a * power;
}

The function will return undefined!

Why? Because JavaScript interprets it as:

Example 5

function myFunction(a) {
  let
  power = 10
  return;
  a * power;
}