JavaScript syntax is the set of rules that define how a JavaScript program is written and interpreted.
; (optional, but recommended)let message = "Hello!";
console.log(message);A statement is a line of code that performs an action.
let x = 10; // Statement 1
x = x + 5; // Statement 2
console.log(x); // Statement 3Multiple statements can be grouped using curly braces {} (called a code block):
{
let name = "John";
console.log(name);
}Comments are used to explain code and are ignored by the browser.
// This is a single-line comment/* This is a
multi-line comment */Variables store data values. You can declare them using:
var name = "Alice";let age = 25;const country = "India";| Keyword | Can Reassign? | Scope Type |
|---|---|---|
| var | Yes | Function-level |
| let | Yes | Block-level |
| const | No | Block-level |
JavaScript is dynamically typed, meaning you don't need to declare a data type.
String - "Hello"Number - 42, 3.14Boolean - true, falseUndefined - variable declared but not assignedNull - intentional absence of valueSymbol - unique identifiers (advanced use cases)BigInt - very large numbers (added in ES2020)ObjectArrayFunctionlet name = "Tom"; // String
let age = 30; // Number
let isOnline = true; // Boolean
let person = {name: "Tom", age: 30}; // ObjectJavaScript supports several kinds of operators:
+ - * / % ** ++ --= += -= *= /= %= **=== === != !== > < >= <=== checks value only=== checks value and type (recommended)&& || !true && false // false
true || false // true
!false // truelet fullName = "John" + " " + "Doe"; // "John Doe"let result = "5" + 2; // "52" (number 2 converted to string)Number("10"); // 10
String(20); // "20"
Boolean(0); // falseUsed to check the type of a variable.
typeof "hello"; // "string"
typeof 5; // "number"
typeof true; // "boolean"
typeof undefined; // "undefined"
typeof null; // "object" (a quirk in JavaScript)Create a small script that:
let and constconsole.log()Example:
let a = 10;
let b = 5;
let sum = a + b;
console.log("Sum:", sum);
console.log("Are both greater than 0?", a > 0 && b > 0);