DEV Community

astghikaboyan
astghikaboyan

Posted on

Variables

Variable is a way to store data value to use it later. Variables can be declared using var, let and const. The var keyword is much older as compared to let and const, which were added to JavaScript in 2015. One of the main differences between var and let is that variables declared with var can be re-declared but variables defined with let cannot. Now let is used more than var.

Variables have names, which are created by the user. Name of the variable can contain letters, numbers, underscores and dollar signs but they cannot start with numbers 0 – 9. Names are also case sensitive which means that the same word with lowercase and uppercase are not the same.
Variables can contain different types of information: numbers, strings and booleans. These are the primitive data types in Javascript. String is used with double or single quotes and contains a group of characters. Boolean data type has two values – true and false.
Here is how variables are declared:
var name_student = “Astghik”;
let auaStudent = true;
const age = 18;

If we want to check the data type of “Astghik”, true or 18, we should use typeof() in the following way:
typeof(“Astghik”) – this will return string
typeof(true) – will return boolean
typeof(18) – will return number

When using the const keyword the variables cannot be changed but we can change the value of the variables declared with let or var. For example:
let course = “Intro to CS”;
but then we want to change the variable course, so we write:
course = “Intro to OOP”;
In this way we assigned a new value to the variable(assignment of the variable).

We can also do mathematical operations inside the variables:
let a = 5+6;
console.log(a); //this will return 11.
Javascript also understands the parenthesis, so if we write:
var b = 3*(2-1); // it will first do the operation inside the parenthesis and then the multiplication, so if we console.log it:
console.log(b) // we will get 3
Besides doing addition the “+” sign is also used to connect strings.
let my_name = “Astghik” + “ Aboyan”;
console.log(my_name) // returns Astghik Aboyan.
We can also connect strings with numbers. The “+” sign will first convert the number into a text and then add it to the string.
let student = “Ugrad” + 26;
console.log(student); // which returns Ugrad26

Scope determines the visibility of the variables. In JS, the variables are declared in local scope or in global scope.
Local variables are declared inside the function. They are not visible outside the function in which they were declared.
Global variables are variables that are defined outside of any function. They are visible from any point in the JavaScript code, and even within any function.
Var blogpost = “Hello”; // global variable
function print() {
var topic = “variables”;
alert(blogpost + topic); // can access to global and local variables
}
print();
alert(blogpost); // can access to global variable
alert(topic); // cannot access to local variable

Top comments (0)