JavaScript Syntax

Understanding the basics of JavaScript syntax

JavaScript Syntax

JavaScript syntax is the set of rules that define how JavaScript programs are constructed. Understanding these rules is crucial for writing correct and effective JavaScript code.

Basic Syntax Rules

  • Statements: JavaScript statements are separated by semicolons (;).
  • Case Sensitivity: JavaScript is case-sensitive. For example, 'myVariable' and 'myvariable' are different.
  • White Space: JavaScript ignores multiple spaces and line breaks.
  • Comments: Use // for single-line comments and /* */ for multi-line comments.

Variables

Variables are declared using 'var', 'let', or 'const':

var x = 5;
let y = 10;
const PI = 3.14;

Data Types

JavaScript has several data types:

  • Number
  • String
  • Boolean
  • Object
  • Array
  • Null
  • Undefined

Functions

Functions are defined using the 'function' keyword:

function greet(name) {
return "Hello, " + name + "!";
}

Objects

Objects are defined using curly braces:

let person = {
name: "John",
age: 30,
city: "New York"
};

These are just the basics of JavaScript syntax. As you progress, you'll learn more complex structures and patterns.