JavaScript Essentials: Getting Started - Basic Syntax
This document covers the fundamental syntax of JavaScript, providing a foundation for building more complex programs.
1. Statements & Semicolons
- Statements: JavaScript code is built from statements. A statement represents an action to be performed.
- Semicolons (;): Statements are typically terminated by a semicolon. While JavaScript has Automatic Semicolon Insertion (ASI), it's best practice to explicitly use semicolons to avoid unexpected behavior.
let message = "Hello, world!"; // Statement 1
console.log(message); // Statement 2
2. Variables
- Declaration: Variables are used to store data. They are declared using
let,const, orvar(avoidvarin modern JavaScript).let: Used for variables that may be reassigned.const: Used for variables whose value should not be changed after initialization.
- Initialization: Assigning a value to a variable.
- Naming Rules:
- Variable names are case-sensitive (
myVaris different frommyvar). - They must start with a letter, underscore (
_), or dollar sign ($). - They can contain letters, numbers, underscores, and dollar signs.
- Avoid using reserved keywords (e.g.,
let,const,function,if).
- Variable names are case-sensitive (
let age = 30; // Declare and initialize with a number
const name = "Alice"; // Declare and initialize with a string (cannot be reassigned)
let isStudent; // Declare without initialization (value is undefined)
isStudent = true; // Assign a value later
3. Data Types
JavaScript has several built-in data types:
- Number: Represents numeric values (integers and floating-point numbers).
let integer = 10; let decimal = 3.14; - String: Represents textual data, enclosed in single (
') or double (") quotes.let greeting = "Hello"; let message = 'JavaScript is fun!'; - Boolean: Represents truth values:
trueorfalse.let isValid = true; let isFinished = false; - Undefined: Represents a variable that has been declared but not assigned a value.
let x; console.log(x); // Output: undefined - Null: Represents the intentional absence of a value.
let emptyValue = null; - Symbol (ES6): Creates unique identifiers. Less commonly used in basic scenarios.
- Object: A complex data type that can store collections of key-value pairs. (Covered in more detail later)
4. Operators
Operators perform operations on values (operands).
- Arithmetic Operators:
+,-,*,/,%(modulus),**(exponentiation)let sum = 5 + 3; // 8 let difference = 10 - 4; // 6 let product = 2 * 6; // 12 let quotient = 15 / 3; // 5 let remainder = 17 % 5; // 2 let power = 2 ** 3; // 8 - Assignment Operators:
=,+=,-=,*=,/=,%=let x = 5; x += 2; // Equivalent to x = x + 2; x is now 7 - Comparison Operators:
==(equal to - loose equality),===(equal to - strict equality),!=(not equal to - loose inequality),!==(not equal to - strict inequality),>,<,>=,<=console.log(5 == "5"); // true (loose equality - type coercion) console.log(5 === "5"); // false (strict equality - no type coercion) - Logical Operators:
&&(AND),||(OR),!(NOT)let a = true; let b = false; console.log(a && b); // false console.log(a || b); // true console.log(!a); // false
5. Comments
Comments are used to explain code and are ignored by the JavaScript interpreter.
- Single-line comments: Start with
//. - Multi-line comments: Start with
/*and end with*/.
// This is a single-line comment
/*
This is a
multi-line comment.
*/
6. Basic Input/Output
console.log(): Used to display output in the browser's developer console or the terminal (Node.js).prompt(): (Browser only) Displays a dialog box that prompts the user for input. Returns a string.
console.log("Hello, world!");
let userName = prompt("Please enter your name:");
console.log("Hello, " + userName + "!");
7. Whitespace
JavaScript is relatively forgiving with whitespace (spaces, tabs, newlines). However, using consistent indentation makes code more readable.
This provides a basic overview of JavaScript syntax. As you progress, you'll learn about more advanced concepts like functions, objects, arrays, and control flow. Remember to practice writing code to solidify your understanding!