By the end of this lesson, learners will:
Syntax is like grammar for a programming language.
Itโs a set of rules and structure that determines how code must be written so the computer can understand and execute it.
Just like a sentence in English has a subject and verb, JavaScript statements must follow certain rules.
A statement is a single instruction in JavaScript โ like a sentence in English.
๐ Example:
let message = "Hello, Codevisionz!";
Here, weโre telling JavaScript to:
message
"Hello, Codevisionz!"
to it;
)Semicolons are used to end a statement in JavaScript.
๐ Example:
let x = 5;
let y = 10;
console.log(x + y);
๐น JavaScript usually allows omitting semicolons, but itโs good practice to use them to avoid unexpected behavior.
JavaScript is case-sensitive โ this means:
let name = "Alice";
let Name = "Bob";
These are two different variables!
๐ Best Practice: Stick to camelCase for variables (like userName
, totalAmount
).
{}
for Code BlocksCurly braces are used to group multiple statements into one block โ like in functions, loops, or conditionals.
๐ Example:
if (true) {
console.log("This will run!");
}
Everything inside {}
is treated as a single block of code.
While not required by JavaScript, indentation (spacing code to show structure) is critical for readability.
๐ Example:
if (score > 50) {
console.log("You passed!");
} else {
console.log("Try again.");
}
Use 2 or 4 spaces consistently to indent blocks.
Comments let you explain your code. JavaScript ignores comments when running the code.
// This is a comment
/*
This is a
multi-line comment
*/
โ Use comments to:
// This is a simple calculation
let a = 5;
let b = 3;
let result = a + b;
console.log("The result is: " + result); // Output: The result is: 8
Mistake | Why Itโs Wrong | Fix |
---|---|---|
Let x = 5; | Incorrect capitalization (Let is not recognized) | Use let |
console.log("Hello') | Missing closing quote | Use matching quotes: "Hello" |
if x > 5 console.log("Yes"); | Missing parentheses {} and () | Correct: if (x > 5) { console.log("Yes"); } |
Concept | Explanation |
---|---|
Statement | A line of code that does something |
Semicolon | Ends a statement (; ) |
Curly Braces {} | Group multiple statements |
Case-Sensitive | username โ UserName |
Comment | Notes inside code ignored by the computer |
Indentation | Spacing to improve readability |