JavaScript is a lightweight, interpreted language because of its simplicity. Interpreters typically execute it in web browsers or server-side platforms like Node.js. While JavaScript is interpreted, modern engines can optimize it.
In JavaScript, we can declare variables using var
, let
, and const
keywords. These keywords are used to declare different kinds of variables. Here is the overview of each kind of keyword:
var
: It declares a variable globally or locally to an entire function, regardless of block scope.
let
: It declares block-scoped variables, which are limited to the block, statement, or expression where they are defined.
const
: It declares a constant (unchangeable) variable. The value must be assigned when the variable is declared and cannot be reassigned.
Note: Read more about the difference between
var
,let
, andconst
in this Answer.
We can assign the variable a value using the assignment operator, =
. The syntax of declaring and assigning variable a value is:
keyword variable_name = value
The following examples demonstrate how to declare and assign different kinds of variables to the values.
// Using varvar a = 5;console.log("Value of a:", a);// Using letlet b = "Hello";console.log("Value of b:",b);// Using constconst c = 3.14;console.log("Value of c:",c);
In the code above:
Lines 2–3: It assigns a
value, 5
, using var
and prints the value.
Lines 6–7: It assigns b
value, Hello
, using let
and prints the value.
Lines 10–11: It assigns c
value, 3.14
, using const
and prints the value.
We can declare and assign multiple values using a single keyword. For example:
let firstName = "User",lastName = "1",age = 20;console.log("Values:", firstName, lastName, age);
The code above declares three variables using let
and prints them on a single line.
We can declare the variables at the start and assign them values in the middle of the code if we want. For example:
let variable;console.log("Value:", variable);variable = "New value";console.log("Value:", variable);
In the code above, we declare a variable. Its value is undefined
at first. When we assign a value, it gets stored in it.
The variable names are case-sensitive, meaning that the first letter of a variable name can be uppercase or lowercase and are considered distinct. It’s a common convention in JavaScript to use camelCase for variable names, where the first letter of each word after the first is capitalized. We can use an underscore (_) or dollar sign ($) to start the variable name. Also, spaces are not allowed in variable names. Let’s look at a few examples below:
let $myVariable = "Hey";let _anotherVariable = 42;let firstName = "User";let lastName = "2";console.log("Value:", $myVariable);console.log("Value:", _anotherVariable);console.log("Value:", firstName + " " + lastName);
The code above shows some valid variable names and prints their values to show the validity.
To conclude, variables can be declared and assigned values using keywords like var
, let
, or const
with the assignment operator. By utilizing these declarations effectively, developers can efficiently manage data and control the flow of their programs.
Free Resources