JavaScript Debugging - Exercise 1

Buggy Code:

javascript

let messge = "Hello";
console.log(message);

The variable is declared as messge but used as message. This is a typo. The correct variable name should be consistent. Fix: rename the variable to message.

Fixed Code:

javascript

let message = "Hello";
console.log(message);

Buggy Code:

javascript

console.log(x);
let x = 10;

Variables declared with let are not hoisted in the same way as var. Accessing x before its declaration causes a ReferenceError. Fix: move the declaration before the console.log.

Fixed Code:

javascript

let x = 10;
console.log(x);

Buggy Code:

javascript

let age = 18;
if (age = 18) {
  console.log("You are 18");
}

The code uses a single = (assignment) instead of === (strict equality). This assigns 18 to age instead of comparing. Fix: use ===.

Fixed Code:

javascript

let age = 18;
if (age === 18) {
  console.log("You are 18");
}

Buggy Code:

javascript

function add(a, b) {
  a + b;
}
console.log(add(2, 3));

The function is missing the return keyword. Without it, the function returns undefined by default.

Fixed Code:

javascript

function add(a, b) {
  return a + b;
}
console.log(add(2, 3));

Buggy Code:

javascript

let firstName = "John";
let lastName = "Doe";
console.log("Full name: " + first_name + " " + lastName);

The variable is declared as firstName (camelCase) but used as first_name (snake_case). JavaScript variable names are case-sensitive.

Fixed Code:

javascript

let firstName = "John";
let lastName = "Doe";
console.log("Full name: " + firstName + " " + lastName);

Buggy Code:

javascript

let i = 0;
while (i < 5) {
  console.log(i);
}

The loop variable i is never incremented inside the loop body, so the condition i < 5 is always true.

Fixed Code:

javascript

let i = 0;
while (i < 5) {
  console.log(i);
  i++;
}

Buggy Code:

javascript

let colors = ["red", "green", "blue"];
console.log(colors[3]);

Arrays in JavaScript are zero-indexed. The array has indices 0, 1, and 2. Accessing index 3 returns undefined because it is out of bounds. Fix: use index 2 to access the last element, or use colors[colors.length - 1].

Fixed Code:

javascript

let colors = ["red", "green", "blue"];
console.log(colors[2]); // "blue"

Buggy Code:

javascript

let num = "5";
if (num === 5) {
  console.log("Equal");
} else {
  console.log("Not equal");
}

The strict equality operator === checks both value and type. Here num is a string "5" and 5 is a number, so they are not strictly equal. Fix: either use == for loose comparison or convert the type.

Fixed Code:

javascript

let num = "5";
if (Number(num) === 5) {
  console.log("Equal");
} else {
  console.log("Not equal");
}

Buggy Code:

javascript

let person = {
  name: "Alice"
  age: 25
};

Object properties must be separated by commas. There is a missing comma after "Alice".

Fixed Code:

javascript

let person = {
  name: "Alice",
  age: 25
};

Buggy Code:

javascript

let greeting;
console.log(greeting);
greeting = "Hello";

The variable greeting is declared but not assigned a value before the console.log call. In JavaScript, uninitialized variables have the value undefined. Fix: assign the value before logging.

Fixed Code:

javascript

let greeting = "Hello";
console.log(greeting);