Basics
Variables, Data Types, and Operators
You've decided to learn JavaScript - great choice! Before you can start building complex web applications, you need to understand the fundamental building blocks of the language. This section will cover the essential basics: variables, data types, and operators. Think of these as the alphabet and basic grammar rules of JavaScript.
Variables
A variable is a named storage location in your computer's memory that you can use to store data. You can think of it as a labeled box where you can put different values.
In JavaScript, you declare variables using the let
, const
, or var
keywords:
let
: Declares a block-scoped variable. This means the variable is only accessible within the block of code where it's defined (e.g., inside anif
statement or afor
loop).let
is the preferred way to declare variables in modern JavaScript.
const
: Declares a block-scoped constant. A constant is a variable whose value cannot be changed after it's initialized. Useconst
for values that should never be reassigned.
var
: Declares a function-scoped variable. This means the variable is accessible throughout the entire function where it's defined (or globally if it's defined outside of any function).var
is an older way to declare variables, and it's generally recommended to uselet
orconst
instead. It doesn't follow the current scope standards.
Recommendation: Use const
for variables whose values should not change and let
for variables whose values may change.
Avoid using var
in modern JavaScript.
Data Types
A data type specifies the kind of value that a variable can hold. JavaScript has several built-in data types:
- Number: Represents numeric values, both integers and floating-point numbers.
- String: Represents textual data.
Strings are enclosed in single quotes (
'
) or double quotes ("
).
- Boolean: Represents a logical value that can be either
true
orfalse
.
- Undefined: Represents a variable that has been declared but has not been assigned a value.
- Null: Represents the intentional absence of a value.
- Symbol: Represents a unique and immutable identifier (introduced in ECMAScript 2015). Symbols are often used as keys in objects.
- Object: Represents a collection of key-value pairs. Objects are used to store more complex data structures.
- Array: Represents an ordered list of values.
Operators
Operators are symbols that tell JavaScript to do something with values.
- Math Operators: Used for calculations.
+
(add)-
(subtract)*
(multiply)/
(divide)
- Comparison Operators: Used to compare values.
==
(equal to)!=
(not equal to)>
(greater than)<
(less than)
- Logical Operators: Used to combine or modify boolean expressions.
&&
(logical AND)||
(logical OR)!
(logical NOT)
- String Operators:
+
(concatenation): Used to concatenate strings.
- Ternary Operator: A shorthand way of writing an
if...else
statement.
Control Flow: if...else
and Loops
if...else
Statements
if...else
statements allow you to execute different blocks of code based on a condition.
You can also chain if...else if...else
statements for multiple conditions:
Loops
Loops allow you to repeat a block of code multiple times.
JavaScript provides several types of loops: while
, for
, and forEach
.
while
Loop: Thewhile
loop executes a block of code as long as a condition is true.
for
Loop: Thefor
loop is commonly used to iterate a specific number of times.
forEach
Loop: TheforEach
loop is used to iterate over the elements of an array.
You can also use arrow function syntax for a more concise forEach
loop:
Practice and Experiment
Understanding variables, data types, operators, if...else
statements and loops is essential for writing any JavaScript code.
Practice using these concepts in different scenarios.
The more you experiment, the better you'll understand how they work!
Last updated on