JavaScript Basics & Syntax

JavaScript is a versatile programming language primarily used for web development. Understanding its basic syntax is essential for writing clear and effective code.

Illustration of javascript basics syntax
Illustration of javascript basics syntax

1. Statements & Semicolons

JavaScript code is made up of statements, which perform actions. Each statement typically ends with a semicolon ;, though JavaScript can often infer it automatically.

💡 Semicolon Usage

It's good practice to end statements with semicolons to avoid unexpected errors due to automatic semicolon insertion.

2. Variables Declaration

Variables store data values. Use let, const, or var to declare them:

  • let: block-scoped, can be reassigned.
  • const: block-scoped, cannot be reassigned.
  • var: function-scoped, older syntax, avoid for modern code.

3. Data Types

JavaScript has several basic data types:

  • Number: integers and floats (e.g., 42, 3.14)
  • String: text enclosed in quotes (e.g., "Hello")
  • Boolean: true or false
  • Null: intentional absence of value
  • Undefined: variable declared but not assigned
  • Symbol: unique and immutable identifier
  • Object: collections of key-value pairs

4. Comments

Use comments to explain code:

  • // for single-line comments
  • /* ... */ for multi-line comments

5. Basic Syntax Examples

📌 Deep Dive: Variable Declaration & Assignment

JAVASCRIPT
let name = "Alice";
const age = 25;
var isStudent = true;
Output
Variables name, age, and isStudent hold data of different types.

6. Expressions and Operators

Expressions combine values and operators to produce a new value. Common operators include:

  • Arithmetic: +, -, *, /, %
  • Assignment: =, +=, -=, etc.
  • Comparison: ==, ===, !=, !==, <, >
  • Logical: && (and), || (or), ! (not)
Comparison Operators
OperatorDescription
==Equality (performs type coercion)
===Strict equality (no type coercion)
!=Inequality (with type coercion)
!==Strict inequality (no type coercion)

7. Case Sensitivity & Naming Conventions

JavaScript is case sensitive. Variable and function names distinguish between uppercase and lowercase letters.

Common naming conventions:

  • camelCase for variables and functions (e.g., totalCount)
  • PascalCase for classes (e.g., MyClass)
  • Avoid using reserved keywords like if, for, class, etc.

⚠️ Reserved Words

Do not use JavaScript reserved keywords as variable or function names to avoid syntax errors.

8. Basic Output

Use console.log() to output information to the browser console for debugging:

📌 Deep Dive: Using console.log()

JAVASCRIPT
let greeting = "Hello, world!";
console.log(greeting);
Output
Hello, world!

💡 Summary

JavaScript syntax includes variables, data types, statements, and operators. Proper use of semicolons, naming, and comments leads to clearer code.