const keywords followed by a variable name:
var productBrand; let productQuantity; const productId;
In this snippet above, we have created three variables ( ie defined or declared them) to store information. Now we can put some data on them to store and access this information later.
var productBrand = "Apple"; let productQuantity = 20; const productId = "Pro Max 15";
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.
These rules are important to make code more readable and understandable. Here are some rules and best practices for naming variables:
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)
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)
- 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.