Js Syntax in JavaScript
? JavaScript Syntax Overview
JavaScript syntax defines the rules for writing valid code. Understanding it helps you write programs that the JavaScript engine can interpret correctly.
Key Syntax Elements
1. Statements
A statement performs an action and ends with a semicolon
;(optional but recommended).
let x = 5;console.log(x);2. Variables
Declared using
var(old),let, orconst.
let name = "Alice";const pi = 3.14;3. Comments
Single-line:
// commentMulti-line:
/* comment */
4. Data Types
Number, String, Boolean, Object, Array, Function,
null,undefined, Symbol, BigInt.
5. Operators
Arithmetic:
+,-,*,/,%Assignment:
=,+=,-=Comparison:
==,===,!=,!==,>,<Logical:
&&,||,!
6. Functions
function greet(name) { return `Hello, ${name}!`;}7. Control Structures
Conditional:
if,else if,else,switchLoops:
for,while,do...while,for...of,for...in
8. Blocks
Group of statements inside
{ }
if (x > 10) { console.log("Large number");}9. Expressions
Produce values:
let total = 5 + 10; // 1510. Semicolons
Optional in many cases due to Automatic Semicolon Insertion (ASI), but recommended for clarity.
Example: Basic Syntax in Action
// Declare variableslet age = 30;const name = "John";// Functionfunction sayHello(user) { if (user) { return `Hello, ${user}!`; } else { return "Hello, guest!"; }}// Use the functionconsole.log(sayHello(name));console.log(sayHello());Want me to dive into any specific part of JavaScript syntax?