Let's start with an example from real life.
You decided to pack all your stuff into different boxes and place them into some storage. You put labels on each box so you can easier find it later, and obviously, the name is descrptive, so you know what is in that box.
Analogy:
The main purpose of variable is to store some information in them. These variables are labeled so whenever we need the information inside we can easily access them. A very common way of saying - access to variable - is reference a variable by its name.
To declare of define a variable, we can use the keyword
let
orconst
andvar
(in oler version of js).
let
let age; // <== this is a variable declaration
let name, email, address; // <== even able to decalre multiple variables in the same line while separating them with a comma.
let
After we declared a variable, the next step is to store some value in it. This process is called Variable initialization
To put some data into the variablem we use the assignment operator =
.
let name; // <== this is a variable declaration
name = 'Hello'; // <== this is a variable initialization
const
const
is used when declaring a variable in which the value will be constant
const name = 'Lina';
name = 'Maria';
// console:
// unknown: "name" is read-only
To summarize, we can use
let
when we expect that the variable will change its value. However, useconst
when you’re sure that the type of the variable will remain unchanged.
A name is simply a reference for a variable.
let companyName = 'Apple';
let age = 3;
String
, number
, array
, object
, etc. Naming Rules
- Names can contain letters (uppercase and lowercase), numbers, and the symbols _ and $.
- The first character of the name can’t be a number.
- When creating a variable with more than one word, we use the "camelCase" style.
- Although we said you could use any English or non-English word, still there’s a bit constraint: there are some reserved keywords that can’t be used, for example: let, class, return, and function are reserved.
let let = 'hello'; // <== error, you can't name variable "let"
The value of a variable declared with let can be changed whenever you want.
To change a value, we juste need to reference the variable by its name and assign a new value:
let favoriteFood;
favoriteFood = "Steak";
console.log(favoriteFood); // <== "Steak"
favoriteFood = "Burger";
console.log(favoriteFood); // <== "Burger"
Basic(primitive) data types in JavaScript:
Non-primitive data types:
To see what is the data type of any variable, you can use the JavaScript operator, typeof
let favoriteFood;
favoriteFood = 'Steak';
console.log('Value: ', favoriteFood, ' Type: ', typeof favoriteFood);
favoriteFood = 20;
console.log('Value: ', favoriteFood, ' Type: ', typeof favoriteFood);
// console:
// Value: Steak Type: string
// Value: 20 Type: number
💡💡 REMEMBER 💡💡
- You can only reassign the variable and changes its value and type if you use
let
when declaring a variable.- To help yourself and others who work or will work on the same code, try to use
cosnt
whenever there is a chance to do so.
Naming variables is hard, and it is, in fact, one of the hardest in programming. You should try yourbest to name variables in a meaningful way.
Variable names like i, j, k, (some exceptions to these), something, object, or variable do not give any indication as to what the value of that variable is. You are more than likely not going to be the only person reading your code.
Wrong naming rules 😟
let x, y, z, zz;
x = 1;
y = 2;
z = (x + y) / 2;
zz = "The average of two numbers is " + z;
console.log(zz);
Good naming rules 😁
var firstNum, secondNum, average, message;
firstNum = 1;
secondNum = 2;
average = (firstNum + secondNum) / 2;
message = "The average of the two numbers is " + average;
console.log(message);
Lessons learned 🎉
- what a variable is,
- how to declare variables,
- how to name variables correctly.