JavaScript’s Syntax & Structure
Every programming language follows a set of grammatical rules that govern how instructions are written and executed. This lesson establishes the fundamental structure you must use to write clear, executable JavaScript code.
Case Sensitivity
JavaScript is a case-sensitive language. This is a crucial rule for beginners to remember: the browser’s JavaScript engine treats lowercase letters and uppercase letters as completely different characters.
| Case Sensitivity Example | Error Status | Reasoning |
| userName | Correct – No Error | This is a standard variable name in JavaScript using camelCase characters. |
| Username | Incorrect – Reference Error | This variable name is different from “userName” due to case sensitivity. |
| console.log() | Correct – No Error | The console.log() built-in function/method is lowercase. |
| Console.Log() | Incorrect – Reference Error | The browser won’t recognize console.log() due to case sensitivity. |
// 1. Declare a variable using a specific case.
const score = 100; //lowercase
const sCore = 200; //capital "C"
// 2. Accessing it with the correct case works.
console.log(score); // Output: 100
console.log(sCore); // Output: 200
console.log(Score); // Output: 'ReferenceError' (due to "S" capitalization).
//ReferenceError is a runtime error that halts the execution of the JavaScript code.JavaScriptComments in JavaScript
In JavaScript, comments are used to add explanatory notes within the code that are ignored by the JavaScript engine during execution. They serve to improve code readability, explain complex logic, or temporarily disable sections of code.
Single-line comments: These comments start with two forward slashes (//). Everything following // on that line is considered a comment and is ignored.
// This is a single-line comment
let x = 10; // This comment explains the variable xJavaScriptMulti-line comments (or Block comments): These comments start with /* and end with */. Everything between these delimiters, spanning multiple lines if necessary, is considered a comment.
/*
This is a multi-line comment.
It can span across several lines
to provide more detailed explanations.
*/
let y = 20;JavaScript“Doc Block” comments are a special form of multi-line comments, often starting with /** and are commonly used for documentation purposes, especially with tools like JSDoc (a JavaScript Documentation Generator).
/**
* This function calculates the sum of two numbers.
* @param {number} a The first number.
* @param {number} b The second number.
* @returns {number} The sum of a and b.
**/
function add(a, b) {
return a + b;
}
console.log(add(10,20)) //Output: 30JavaScriptStatements and Semicolons
A statement is a complete instruction given to the computer—it’s the programming equivalent of a sentence. Every action you perform (like declaring a variable or printing to the console) is a statement.
By convention, every statement should be terminated with a semicolon (;) at the end.
// Statement 1: Declares and initializes the 'name' variable.
const name = "Luna";
// Statement 2: Declares and initializes the 'greeting' variable.
let greeting = "Hello";
// Statement 3: Logs the concatenated result to the console.
console.log(greeting + " " + name); // Output: Hello LunaJavaScriptWhile JavaScript has a feature called Automatic Semicolon Insertion (ASI) that sometimes adds them for you, relying on it can lead to confusing bugs. Always place the semicolon yourself for predictable, professional code.
Strict Mode
Strict mode in JavaScript is a way to opt in to a restricted variant of the language, which applies stricter rules and changes or disables certain problematic or error-prone behaviors. It aims to make JavaScript code more robust, secure, and easier to debug by catching common coding mistakes and enforcing better practices.
You can enable strict mode by adding the string literal "use strict"; at the beginning of a script or a function.
// Strict mode for the entire script
"use strict";
// Your JavaScript code here
// Strict mode for a specific function
function myStrictFunction() {
"use strict";
// Function-specific code here
}JavaScriptDynamic vs. Static Typing
Typing determines when and how a variable’s data type is checked. This is one of the most fundamental differences between JavaScript and other popular languages.
JavaScript is a dynamically typed programming language, which means:
- You do not have to tell the variable what type of data it holds when you create it.
- A single variable can change its type throughout the program’s execution (e.g., changing from holding a number to holding a string).
- Type checking happens at runtime (while the code is executing).
Code Example (Dynamic Typing in JS):
let myData = 5; // myData starts as a Number
console.log(typeof myData); // Output: number
// The type is changed easily and dynamically:
myData = "Five"; // myData is now a String
console.log(typeof myData); // Output: stringJavaScriptStatic Typing (Comparison with TypeScript):
In a statically typed language (like Java, C++, or TypeScript, a language built to extend JavaScript):
Compile-Time Check: Type errors are caught early in the development process (compile time), making large applications generally safer and easier to maintain.
Mandatory Declaration: You must define the variable’s type when you declare it (number data type variable declaration example: let age: number = 20;).
Fixed Types: Once declared, the variable can only ever hold that type of data. If you try to put a string into an integer variable, the code will fail before it runs.
While JavaScript’s dynamic typing is great for small, fast-paced projects, many professional teams use TypeScript to gain the error-checking benefits of static typing for large, complex applications.




