Javascript Variables
By gobrain
Jul 11th, 2024
Variables are main component of any programming language that allows us to store a wide range of data and access them later, and javascript is not an exception.
Imagine that you are writing javascript for an e-commerce site. In this case, products, quantities, customers, credit card information are all data on this site and variables are used to store like these pieces of information in memory.
Now let's see examples of how to create new variables and use them in javascript.
How To Create Variables
To create a variable in javascript, you can use the var
, let
, or const
keywords followed by a variable name:
var productBrand = "Apple";
let productQuantity = 20;
const productId = "Pro Max 15";
After declaring variables, each variable will represent the data it holds. For example, when you use the variable productQuantity somewhere in your code, it will represent the value 20.
Additionally, You can create variables they initally do not hold any data and you can assign data to them after they have been declared.
For example,
var productBrand;
let productQuantity;
const productId;
productBrand = "Apple"
productQuantity = 20
productId = 4
Using Variables
Once we create some variables and put information in then, we can now use them in different parts of our javascript application.
let width = 20;
let height = 20;
let area = width * height;
console.log(area) // 400
In this example, we have three variables; The width and height variables store the side lengths of a square, and the area variable stores the area of the square. This example shows that after you declare some variables, you can use them to create another variable or to get information inside them.
Naming Variables
As you can see, we have given different names to each variable. We have some restrictions in giving these names and best practices without obligation mostly used by the javascript community.
These rules are important to make code more readable and understandable. Here are some rules and best practices for naming variables:
Rules
let product1 = "Phone"; ✔
let product-1 = "Phone"; ✘
let 9product = "PC" ✘
let while = 4 ✘
- Variable names can only contain letters, numbers and underscores
- Variable names cannot start with a number
- Reserved keywords cannot be used as variable names (if, for, while)
- Variable names are case sensitive: myVariable, myvariable and MyVariable are all different variables in JavaScript.
Best Practises
let a = "MacBook"; ✘
let computer = "MacBook"; ✔
let phonenumber = "0234058"; ✘
let phoneNumber = "0234058"; ✔
let pi = 3.14; ✘
const PI = 3.14; ✔
- Variable names should describe the data they store
- It is common to use camelCase for variable names (productId, phoneNumber )
- If a value of a variable will not change throughout the program, it is common to use uppercase letters for its name (PI_NUMBER, MAX_NUMBER)
Summary
In this guide we have discussed the variables in javascript. The summary of the guide would be:
- Variables are used to store data on memory
- Variables can be declared with three keywords (var, let, const)
- After creating a variable and putting data into it , it can be used in different sections of the application
- We have some restrictions that is necessity and best practises to make code more readable for giving name to a variable
Learning requires practice, so you should work on variables locally so that you can gain a more comprehensive understanding of the topic.
Thank you for reading.