Variables and Data Types
Variables are used to store data in JavaScript. They can be declared using the var, let, or const keywords.
var age = 25; // Using var
let name = "John"; // Using let
const PI = 3.14159; // Using const
JavaScript has several data types, including:
- Number: Represents both integers and floating-point numbers.
- String: Represents textual data enclosed in single or double quotes.
- Boolean: Represents a logical value, either
trueorfalse. - Undefined: Represents an uninitialized variable or a non-existent object property.
- Null: Represents an intentionally empty value.
- Object: A collection of key-value pairs, capable of storing complex data structures.
- Symbol: A new data type introduced in ES6, used to create unique identifiers.
Operators
JavaScript supports various operators for performing calculations, comparisons, and other operations:
- Arithmetic Operators:
+,-,*,/,%,++,-- - Assignment Operators:
=,+=,-=,*=,/=,%= - Comparison Operators:
==,===,!=,!==,>,<,>=,<= - Logical Operators:
&&,||,! - Bitwise Operators:
&,|,^,~,<<,>>,>>>
Statements and Expressions
Statements are the basic units of execution in JavaScript. They can be separated by semicolons or written on separate lines. Expressions are a combination of values, variables, operators, and function calls that produce a value.
let x = 5; // Statement
let y = x + 3; // Expression
Comments
Comments are used to add explanatory notes or temporarily disable code in JavaScript. There are two types of comments:
- Single-line comments: Preceded by
//// This is a single-line comment - Multi-line comments: Enclosed between
/*and*//* This is a multi-line comment */
In the next chapter, we'll explore control flow statements and loops, which are essential for creating dynamic and interactive programs.