Syntax, Statements & Error Handling

๐ŸŸจ JavaScript (ES2026+) ๐ŸŸข Lesson 2 ๐Ÿ“‚ Phase 01: JavaScript Fundamentals ๐Ÿ“… 2026 Edition
๐Ÿ“Œ Covered in this lesson: Comments (Single & Multi-line) ยท Statements ยท Semicolons & ASI ยท Case Sensitivity ยท Syntax Errors ยท Runtime Errors ยท Logical Errors

In this second lesson of Phase 1, we delve into the core grammar rules of JavaScript: Statements, Semicolons & Automatic Semicolon Insertion (ASI), Case Sensitivity, Comments, and a comprehensive breakdown of the 3 categories of Programming Errors (Syntax vs Runtime vs Logical errors) with practical debugging strategies.

1Comments in JavaScript (Code Documentation)

Comments developer rase logic ni explain cheyyadaniki mariyu code readability penchadaniki vadathamu. JS Engine execution time lo comments ni completely ignore chesthundi:

Comment TypeSyntaxUsage
1. Single-Line Comment // Your comment here Short explanations on a single line.
2. Multi-Line Comment /* Line 1
Line 2 */
Longer explanations spanning across multiple lines.
3. JSDoc Comment /** JSDoc description
* @param {string} name */
Used by VS Code and documentation generators for type hints and autocomplete.
2Statements, Semicolons & Automatic Semicolon Insertion (ASI)

JavaScript lo prati execution instruction ni Statement antaru. Statements sequential ga execute avthayi.

Semicolons (;) & ASI Gotchas

Statements เฐšเฐฟเฐตเฐฐ Semicolon ; unchadam best practice. JavaScript lo ASI (Automatic Semicolon Insertion) เฐ…เฐจเฑ‡ feature undhi โ€” meeru semicolon rayaka poina browser automatically insert cheskuntundhi. Kani ASI ni completely trust cheste konni hidden bugs edhuravthayi:

JavaScript โ€” ASI Return Bug Trap โ–ถ Run Code
"use strict";

function getUser() {
    // Dangerous ASI Trap:
    // JS inserts semicolon right after return!
    return
    {
        name: "Balaji"
    };
}

console.log("User Object:", getUser()); // Prints undefined instead of Object!
3Case Sensitivity in JavaScript

JavaScript is strictly case-sensitive. Capital and small letters completely different identifiers ga count avthayi:

  • let score = 100; and let Score = 200; are two separate variables in memory.
  • JavaScript keywords MUST be in lowercase: function, if, const, return. (e.g. Function or IF will throw a SyntaxError).
4The 3 Types of Errors (Syntax, Runtime & Logical Errors)

1. Syntax Errors (Parse-Time Errors)

JavaScript grammar rules ni violate chesinappudu JS engine code parse/tokenize chesetappude (before execution) SyntaxError throw chesi execution completely stop chesthundi.

Examples:
  • Missing closing parenthesis ) or bracket.
  • Unclosed String literal (missing quote).
  • Keyword typo (e.g. fuction myFunc()).

2. Runtime Errors (Exceptions)

Code syntax correct gaane parsing complete avthundhi. Kani script run ayye time lo illegal operation (e.g. calling an undefined variable or property on null) jariginappudu script crash ayyi Exception throw chesthundi.

Examples:
  • ReferenceError: x is not defined (Accessing un-declared variable).
  • TypeError: Cannot read properties of null (Calling method on null/undefined).

3. Logical Errors (Silent Bugs)

Program 0 error messages tho run avthundhi. Kani developer logic/formula wrong unna karanam ga wrong output vasthundhi (hardest to detect).

JavaScript โ€” Error Types Demo โ–ถ Run Code
"use strict";

let num1 = 20;
let num2 = 10;

// Correct Sum
let sum = num1 + num2;
console.log("Sum:", sum);

// Logical Error Example:
// Expected average of 20 and 10 is 15.
let wrongAvg = num1 + num2 / 2;    // Evaluates to: 20 + 5 = 25 (BUG!)
let correctAvg = (num1 + num2) / 2; // Evaluates to: (30)/2 = 15 (CORRECT!)

console.log("Wrong Average (Logical Bug):", wrongAvg);
console.log("Correct Average:", correctAvg);
๐Ÿ’ป Try It Yourself โ€” Debugging Exercise

Fix the logical bug in calculating the total discount below:

JavaScript Debugging โ–ถ Run Code
"use strict";

let price = 500;
let quantity = 2;
let discountPercent = 10; // 10%

let total = price * quantity; // 1000
let discount = (total * discountPercent) / 100; // 100
let finalBill = total - discount;

console.log("Total Amount: Rs." + total);
console.log("Discount: Rs." + discount);
console.log("Final Bill: Rs." + finalBill);
Run Code in Our Compiler โ†’
OC
Written and reviewed by Our Compiler Technical Team ยท Updated for JavaScript ES2026+