Learn JavaScript
Lesson 1 - Introduction To JavaScript
Lesson 3 - Control Flow
Lesson 4 - Functions
Lesson 5 - Basic Data Structures
Lesson 6 - Basic String Operations
Lesson 7 - Basic Array Operations
Lesson 8 - Exception Handling
Lesson 9 - Packages
Lesson 10 - User Input
Variables are fundamental building blocks in programming, acting as containers that store data values. These values can be manipulated and changed throughout the execution of a program.
A JavaScript variable consists of three parts: the variable keyword, its name, and its value.
var myVar = 123;
In the example above, we are declaring a variable using the var
keyword, with the name myVar
. We are also assigning its value to be 123
. Var
is one of three keywords that can be used in JavaScript to declare a variable, the other two being let
and const
.
We will discuss this in more detail in the next chapter, but before diving deeper into variables, it's essential to understand the difference between variable declaration and definition.
In the example above, we both declared and initialized the variable myVar
in one line. However, it's important to note that this actually involves two concepts.
Declaration refers to the process of specifying a variable's name, but it doesn't necessarily assign a value to it. In JavaScript, declaring a variable can be done using the var
, let
, or const
keywords.
For example, we can just declare a variable like this:
var myVar;
Initialization, on the other hand, involves assigning a value to the variable.
For example, for the previously declared variable myVar
, we can simply assign a value like this:
myVar = 123;
Using Variables
Once a variable has been defined, i.e., both declared and initialized, we can use its value later on in the program. Let’s have a look at an example of this.
var firstName = "John"; var lastName = "Joe"; console.log(firstName + " " + lastName);
In the example above, we first define two variables firstName
and lastName
, and then print their values using the console.log
statement.
Data types in JavaScript
In many other programming languages, you can specify the type of data a variable can hold, such as integer
, float
, or string
. However, JavaScript does not require you to explicitly define data types. Let's look at how to define different types of variables.
Declaring a variable with a numeric value:
var num = 10; let num1 = 10.55; console.log(num); // Outputs: 10 console.log(num1); // Outputs: 10.55
Declaring a variable with a character value:
let ch = 'a'; console.log(ch); // Outputs: a
Declaring a variable with a string value:
let txt = "This is a text."; console.log(txt); // Outputs: This is a text.
Now, let's see how and where we use variables in a program. The following example demonstrates adding two numbers and displaying the result:
let a = 10; let b = 5; let result = a + b; console.log(result); // Outputs: 15
In this example, we performed an addition operation between the variables a
and b
. We then stored the result in the variable result
and printed it using console.log()
.
You can experiment by assigning different values to variables a
and b
, and then observe the resulting output.