JavaScript Syntax

JavaScript Statements

A JavaScript program is a sequence of statements, each performing a task:

Loading...
Output:

Semicolons

Semicolons are used to separate statements, though JavaScript can often infer them. Best practice is to always use semicolons:

let name = "Alice";
console.log(name);

Case Sensitivity

JavaScript is case-sensitive. The following two variables are different:

Loading...
Output:

Be consistent with your naming conventions.

Comments

Use comments to document your code:

// This is a single-line comment

/*
  This is a 
  multi-line comment
*/

Whitespace and Indentation

Whitespace is ignored, but use indentation for clarity:

function greet() {
  let name = "John";
  console.log("Hello, " + name);
}

Code Blocks

Curly braces {} define blocks of code, especially in functions, loops, and conditionals:

if (x > 0) {
  console.log("Positive");
}

Need Help?

Ask the AI if you need help understanding or want to dive deeper in any topic