JavaScript Basics: Variables

JavaScript is loosely typed programming language. This means there are variables and types but these aren’t very strict and we can easily swap them around. For example, a variable can hold a number value and then we can assign a string to the same variable:
var age = 18; // Assign a number
age = 'eighteen'; // Assign a string
In strongly typed languages that would be impossible.
Variable simply holds a value or object. For example, numbers: 1
, 2
, 3
can be the values a variable holds. Variable name must start with _
(underscore), $
(dollar sign) or letter
. Variable name can’t be a reserved keyword.
We can declare a variable by using var
keyword followed by variable name:
var a;
var _b;
var $c;
Technically, variables a
, b
, c
have a value of undefined
.
We can also create a variable and assign a value:
var minAge = 18;
Variable minAge
holds number value of 18
.
Moreover, we can declare a variable and assign undefined
:
var bar = undefined;
In JavaScript we have following types of variables,
primitive (a primitive value is not an object)
number
— numeric value, for example:1
or3.14
string
— string of characters, for example:‘John’
boolean
— logical value oftrue
andfalse
undefined
— default value when a variable was declared, however the actual value wasn’t assigned yetnull
— no valuesymbol
— symbol type
and
object
— all other types are objects
Let’s try to declare them all:
var minAge = 18;
var name = 'John';
var isAlive = true;
var b;
var c = null;
var s = Symbol();
var carObject = {
color: 'red',
year: 1990
}var colors = ['red', 'green', 'blue'];
var f = function() {
return null;
}console.log(typeof minAge); // number
console.log(typeof name); // string
console.log(typeof isAlive); // boolean
console.log(typeof b); // undefined
console.log(typeof c); // object
console.log(typeof s); // symbol
console.log(typeof carObject); // object
console.log(typeof colors); // object
console.log(typeof f); // function
As we can see array
of colors
is a treated as object
.
Interesting case is the null
. Technically, this should produce typeof null === null
. Yet, for historical reasons this gives incorrect value of object
. There was a change proposed to fix it, yet it was rejected. Therefore, we must remember that null
is one of primitive values, its type is null
yet typeof
returns object
.
Another, odd result is function
— functions are objects. Yet, the specification of typeof
returns function
.
One more thing of the past, is the actual var
keyword. We can use still use it. However, since ECMAScript2015 it’s better to use newer let
and const
keyword.
There is a subtle difference between the two. The first one, let
allows to reassign a value multiple time:
let a = 1;
a = 2;
While, const will allow to assign the value only once. However, lets note const shouldn’t be mistaken with constant. The value can change. It can’t be reassigned only. Let’s see this in action:
const a = 1;
a = 2; // TypeError: Assignment to constant variable.
However:
const colors = ['red', 'green', 'blue'];
colors[3] = 'yellow';
console.log(colors); // ["red" , "green" , "blue" , "yellow" ]
We have changed the value but we didn’t use the assignment operator:
const colors = ['red', 'green', 'blue'];
colors = ['red', 'green', 'blue', 'yellow']; // TypeError: Assignment to constant variable.
In modern days JavaScript the safest way to declare variables is to use const
. If we need to change the value then we should use let
keyword. Don’t use var
anymore.