Skip to content

Latest commit

 

History

History
34 lines (23 loc) · 1.94 KB

variables.md

File metadata and controls

34 lines (23 loc) · 1.94 KB

🔓 Variables

Variable is used to store data temporarily in the computer's memory so that we can use them later. Before ES6, var is used to declare a variable and they found some issues using var. Hence, going forward from ES6, the best practice to store data is to declare let. Here's the examples:

var surname = 'Cheong';
let name = 'Yien';
let birthDate = 10;

console.log(surname);
console.log(name);
console.log(birthDate);

In order to print the variables that you have keyed in to the console, you can use the console.log(), and fill up the name of the variable between the parentheses(). For example, console.log(surname) is used to print the surname in the console.


ℹ️ You can press Ctrl + Shift + i to open the console in your browser.


We can actually use the variable type, string, to store a string value.

String name = 'Jeffrey';

This method is also called static typing and is not the best practise as the type of variable is set and cannot be changed in the future.

Thus, letor var is more widely use in declaring a variable as we can change the type of value stored in the variable. This is also called dynamic typing.



◀️ Previous Page : How to write JavaScript in HTML 🚩                             🏡                             ▶️ Next Page : Basics of JavaScript : Constant 🔓