Understanding JavaScript Variables
JavaScript Variables Data Containers: Variables JavaScript variables act as containers for storing data. There are four ways to declare variables in JavaScript: Modern JavaScrip...
JavaScript Variables
Data Containers: Variables
JavaScript variables act as containers for storing data. There are four ways to declare variables in JavaScript:
Modern JavaScript
- Using
let - Using
const
Older JavaScript
- Using
var(Not Recommended) - Automatically (Not Recommended)
From these examples, you will see:
xholds the value 5yholds the value 6zholds the value 11
Variables serve as labels for data values and are used to store data efficiently.
JavaScript Identifiers
Variables are identified with unique names known as identifiers. These names can be brief, like x, y, z, or descriptive, such as age, sum, carName. The rules for constructing identifiers are:
- Names can contain letters, digits, underscores, and dollar signs.
- Names must start with a letter,
$, or_. - Names are case-sensitive (
Xis different fromx). - Reserved words (JavaScript keywords) cannot be used as names.
Numbers cannot be the first character in names, allowing JavaScript to distinguish identifiers from numbers.
JavaScript Underscore (_)
JavaScript treats the underscore as a letter. Identifiers that include _ are valid variable names. It is common practice among programmers to use an underscore at the beginning of a name for "private" variables.
JavaScript Dollar Sign ($)
JavaScript also considers a dollar sign as a letter. Identifiers containing $ are valid variable names. Although not common, the $ is often used by professionals as an alias for primary functions in JavaScript libraries.
Declaring JavaScript Variables
Creating a variable in JavaScript is known as declaring a variable. You can declare a JavaScript variable using the let or const keyword.
Declaring a Variable Using let
Upon declaration, a variable has no value (technically it is undefined). To assign a value, use the equal sign. Typically, a value is assigned during declaration.
Declaring a Variable Using const
Use const when the value should remain unchanged. For instance, the variables price1 and price2 declared with const cannot have their values altered. In contrast, the variable total, declared with let, can have its value changed.
Declaring a Variable Automatically
Variables can be automatically declared when first used, though it's best practice to declare all variables at the start of a script.
Declaring a Variable Using var
Before 2015, the var keyword was standard in JavaScript. The let and const keywords were introduced in 2015 and are now preferred.
When to Use var, let, or const?
- Always declare variables.
- Use
constif the value should not change. - Use
constfor arrays and objects if the type should not change. - Use
letonly whenconstcannot be used. - Avoid
varifletorconstcan be used.
JavaScript Data Types
JavaScript variables can contain eight data types, but primarily focus on numbers and strings. Strings are text enclosed in quotes, while numbers are written without quotes. A number in quotes is treated as a text string.
Single Statement, Multiple Variables
You can declare multiple variables in one statement using let or const and separating them with a comma. Declarations can span multiple lines for clarity.
The Assignment Operator
In JavaScript, the equal sign (=) is an assignment operator, not an equal to operator. Unlike algebra, the expression x = x + 5 is valid in JavaScript. It assigns the result of x + 5 back to x, effectively incrementing x by 5. The equal to operator is written as ==.
JavaScript Arithmetic
JavaScript allows arithmetic operations on variables, similar to algebra, using operators like = and +. You can also add strings, but they will be concatenated. If a number is included in quotes, subsequent numbers are treated as strings and concatenated.