DEV Community

Cover image for Variables and Datatypes
Soloudo Uzoukwu
Soloudo Uzoukwu

Posted on

Variables and Datatypes

Variables and datatypes form the core building blocks of any programming language. This article aims to educate you on a few key concepts involving variables and datatypes in JavaScript.

What is a Variable?

In JavaScript, a variable is simply referred to as a data container or a storage location for data. A good analogy would be to picture variables as pots used to hold different types of food. Variables in JavaScript are known to be loosely typed; this means that a data type does not need to be declared, and any literal value can be assigned to any variable.

Variable declaration

Declaring a variable in JavaScript is the act of creating one. Variables can be declared using one of two keywords, "let" and "const" these words are used before the variable name to define block scope. The let keyword is used to specify that a variable being declared is unrestricted, implying that the data stored in it is changeable. In contrast, the "const" keyword is used to indicate that the variable is restricted and that whatever data it contains can not be changed.

Multiple Variable declarations

It is possible to see a line of code having many variables declared in one statement. This is done by using a comma after each variable.

For example

let sonName = 'boy', dadName = 'Kratos', strength = 100;
Enter fullscreen mode Exit fullscreen mode

Variable Identifiers

These are unique names given to variables, and there are general guidelines that make for better practice when writing these names. They are;

•Variable names should start with a letter (a to z or A to Z),
•The camel case naming convention is strongly recommended (e.g. newValue).
•The use of underscore( _ ), or dollar( $ ) sign is only permitted at the beginning of the variable name(e.g _headcount, _studentName)
•The use of numbers in naming is only allowed at the end of the name and not at the start or any other location(e.g. value2, entry7)
•JavaScript variables are case-sensitive; for example, x and X are different variables.
•Keywords used in JavaScript can not be used as names.

JavaScript Reserved Words

A list of all the reserved words in JavaScript is given in the following table. They cannot be used as JavaScript variables, functions, methods, loop labels, or object names.

abstract else instanceof switch
boolean enum int synchronized
break export interface this
byte extends long throw
case false native throws
catch final new transient
char finally null true
class float package try
const for private typeof
continue function protected var
debugger goto public void
default if return volatile
delete implements short while
do import static with
double in super

Types of Variables

Below you’ll discover there are two types of Variables.
•Global Variables
•Local Variables

Global variables are variables declared outside any function in the JavaScript code; they can be accessed from any function or part of the code.

Local variables are restricted to the functions where they were declared; a local variable is not accessible from anywhere and can only be used within the function it declared.

Variable Initialization

Variables are initialized by assigning data to them; this is done using the assignment operator(=). It can be done from inside or outside a function. In addition, different types of data can be assigned to a variable.

Variable use Cases

Aside from data storage, variables are used to do other things in JavaScript. Their use in arithmetic operations can not be understated. Variables holding a string data type can also be concatenated using the plus (+) operator. Variables can also have functions stored in them.

Data types

Programming languages have a set of data that is supported. For example, in JavaScript, there are two types, primitive and object values.

Primitive values include;

  • Numbers can either be integers or doubles. An integer is represented as a whole number, while doubles include decimals.
  • Strings, which are combinations of character values, represent text. When writing a string in JavaScript, the string characters are enclosed between two quotes, two double quotes or two backticks.
  • Boolean, these only have two values, true and false.
  • Null returns the value null when used; the value null is assigned to reset a variable by emptying it.
  • Undefined, a variable returns the value undefined when a value has not been assigned to it.
  • NaN(Not a Number) is commonly encountered as the result of an arithmetic operation done with the use of non-number values. It is also the only datatype in JavaScript that does not equal itself.

Object values include;

Objects are a collection of properties. The object literal syntax can add or remove properties from the object. Properties can be of type number, string, boolean or other objects.

Declaring and Initializing an Object

Declaring an object in JavaScript is done using either a "let" or "const" keyword followed by the identifier, an assignment operator and two curly braces. All the properties are added in the curly braces by assigning data to a key, each key in the object is separated by a comma and values are assigned using a colon instead of an equal sign.

const shoppingBag = {itemName: Stockfish, itemPrice: 5000, currency: Naira})
Enter fullscreen mode Exit fullscreen mode

Arrays, an array is a unique variable that serves as a list for storing multiple values under the same name.

Declaring arrays and initializing an array

To declare an array, use the “let” or “const” keyword followed by the identifier, an assignment operator and an opening and closing square bracket.

const bodyCount = [];
Enter fullscreen mode Exit fullscreen mode

There are multiple ways to initialize an array, one of which is by assigning the data directly to the specified index in the array the syntax looks like so.

bodycount[0] = 10;
Enter fullscreen mode Exit fullscreen mode

Another way to initialize an array is by using the push method provided by JavaScript. The data added to the array is put in between braces as parameters. This initialization method adds an element to the following index after the last element in the list.

bodyCount.push(10)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)