javascript-course-chapter-03
Chapter 3: JavaScript Fundamentals
Learn the basic building blocks of JavaScript code with simple explanations, examples, output, and comments.
Goal: Understand how JavaScript code is written, read, organized, and executed.
3.1 Syntax
Syntax means the rules of writing JavaScript code. Like English has grammar rules, JavaScript has code writing rules.
Example: Correct JavaScript syntax
// Create a variable named message.
const message = "Hello JavaScript!";
// Print the variable value in the console.
console.log(message);
Explanation: The code uses correct JavaScript syntax: a variable name, equal sign, string value, and console.log().
3.2 Statements
A statement is a complete instruction. JavaScript reads and runs statements one by one.
Example: Multiple statements
// First statement: create a variable.
const name = "Sara";
// Second statement: create another variable.
const age = 12;
// Third statement: print information.
console.log(name + " is " + age + " years old.");
Output:Sara is 12 years old.
Explanation: Each line gives JavaScript one instruction to complete.
3.3 Expressions
An expression is code that produces a value. For example, 5 + 3 produces 8.
Example: Expression result
// This expression calculates a number.
const total = 5 + 3;
// Print the calculated result.
console.log(total);
Explanation: The expression 5 + 3 gives a value, and that value is stored inside total.
3.4 Comments
Comments are notes inside code. JavaScript ignores comments. They help humans understand the code.
Example: Single-line and multi-line comments
// This is a single-line comment.
/*
This is a multi-line comment.
It can explain bigger sections of code.
*/
// Create a course name.
const course = "JavaScript";
// Print the course name.
console.log(course);
Explanation: Comments do not appear in the output. They only explain the code.
3.5 Keywords
Keywords are special words JavaScript already uses. Examples include const, let, if, else, function, return, and class.
Example: Using keywords
// const is a keyword used to create a variable.
const score = 90;
// if is a keyword used to make a decision.
if (score >= 50) {
// This runs when the condition is true.
console.log("Pass");
}
Explanation: JavaScript understands const and if as special commands.
3.6 Identifiers
Identifiers are names you create for variables, functions, classes, and objects. Good identifiers make code easier to read.
Example: Clear identifier names
// studentName is a clear identifier.
const studentName = "Omar";
// mathMark is another clear identifier.
const mathMark = 95;
// Print the values.
console.log(studentName + " got " + mathMark + " in math.");
Output:Omar got 95 in math.
Explanation: Names like studentName and mathMark explain what the values mean.
3.7 Semicolons
A semicolon marks the end of a statement. JavaScript often adds semicolons automatically, but beginners should use them for clarity.
Example: Statements with semicolons
// First statement ends with a semicolon.
const city = "Toronto";
// Second statement ends with a semicolon.
console.log(city);
Explanation: Semicolons make it clear where each instruction ends.
3.8 Case Sensitivity
JavaScript is case-sensitive. This means name, Name, and NAME are different identifiers.
Example: Different letter cases
// This variable uses lowercase letters.
const name = "Ali";
// This variable starts with uppercase N.
const Name = "Sara";
// Print both variables.
console.log(name);
console.log(Name);
Explanation: JavaScript treats name and Name as two different variables.
3.9 Reserved Words
Reserved words are words JavaScript keeps for its own language rules. You cannot use them as variable names.
Example: Avoid reserved words
// Correct variable name.
const studentClass = "Grade 6";
// Wrong example:
// const class = "Grade 6";
// class is reserved, so do not use it as a variable name.
// Print the correct variable.
console.log(studentClass);
Explanation: Use names like studentClass instead of reserved words like class.
3.10 Code Organization
Code organization means keeping your code clean, grouped, and easy to understand. Good organization helps you fix and update code faster.
Example: Organized beginner code
// ===============================
// 1. Store data
// ===============================
const firstName = "Lina";
const subject = "JavaScript";
// ===============================
// 2. Create a function
// ===============================
function makeMessage(name, course) {
// Return a complete sentence.
return name + " is learning " + course + ".";
}
// ===============================
// 3. Use the function
// ===============================
const finalMessage = makeMessage(firstName, subject);
// ===============================
// 4. Show output
// ===============================
console.log(finalMessage);
Output:Lina is learning JavaScript.
Explanation: Organized sections make your JavaScript easier to read, especially when projects become bigger.