Syntax, Statements & Error Handling
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.
Comments developer rase logic ni explain cheyyadaniki mariyu code readability penchadaniki vadathamu. JS Engine execution time lo comments ni completely ignore chesthundi:
| Comment Type | Syntax | Usage |
|---|---|---|
| 1. Single-Line Comment | // Your comment here |
Short explanations on a single line. |
| 2. Multi-Line Comment | /* Line 1 |
Longer explanations spanning across multiple lines. |
| 3. JSDoc Comment | /** JSDoc description |
Used by VS Code and documentation generators for type hints and autocomplete. |
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:
"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!
JavaScript is strictly case-sensitive. Capital and small letters completely different identifiers ga count avthayi:
let score = 100;andlet Score = 200;are two separate variables in memory.- JavaScript keywords MUST be in lowercase:
function,if,const,return. (e.g.FunctionorIFwill throw a SyntaxError).
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.
- 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.
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).
"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);
Fix the logical bug in calculating the total discount below:
"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);