DEV Community

Cover image for JavaScript variables and data types
Tadea Simunovic
Tadea Simunovic

Posted on • Edited on

JavaScript variables and data types

Javascript is what made modern web development possible. Traditionally Javascript has been only used in browser environment know as client-side, however, with new technology knows as Node.js, developers are able to use it on the server-side as well.
Today there is a lot of javascript libraries and frameworks that implement different architectures and help developers build complex applications more easily and faster.

Variables in JavaScript are containers for storing data values.

  • The value stored in a variable can be changed during program execution
  • A variable is only a name given to a memory location, all the operations done on the variable effects that memory location
  • All the variables must be declared before they can be used

A variable can at one moment be a string and at another be a number.
This is because Javascript has a feature called dynamic typing, data types are automatically assigned to variables. This means that javascript automatically figures out if the variable is a string, number or undefined.

let message = "Hello World"
message = 1234;
Enter fullscreen mode Exit fullscreen mode

Let's have a brief overview of six data types in Javascript

The Number Data Type

The number data type is used to represent positive or negative numbers with or without decimal place, or numbers are written using exponential notation e.g. 1.5e-4 (equivalent to 1.5x10-4).

var a = 20; //integer 
var b = 20.5; // floating-point number
var c = 4.25e+6; // exponential notation, same as 4.25e6 or 4250000
var d = 4.25e-6; // exponential notation, same as 0.00000425
Enter fullscreen mode Exit fullscreen mode

Number data type also includes some special values which are: Infinity, -Infinity and NaN. Infinity is the result of dividing a nonzero number by 0, while NaN represents a special Not-a-Number value. It is a result of an invalid or an undefined mathematical operation.

alert(20 / 0);  // Output: Infinity
alert(-20 / 0); // Output: -Infinity
alert(20 / -0); // Output: -Infinity

alert("Hello World" / 2); // Output: NaN
alert("Hello World" / 2 + 10); // Output: NaN
Enter fullscreen mode Exit fullscreen mode

The String Data Type

The string data type is used to represent textual data. Strings are created using single or double quotes surrounding one or more characters.

var a = 'Hello World!';  // using single quotes
var b = "Hello World!";  // using double quotes
var c = "Let's go."; // single quote inside double quotes
var d = 'Book title is "Code", I like it.'; // double quotes inside single quotes
var e = 'We\'ll learn how to code together.'; // escaping single quote with a backslash
Enter fullscreen mode Exit fullscreen mode

The Boolean Data Type

The Boolean data type can hold only two values true or false. It is typically used to store values like yes (true) or no (false).
Boolean values also come as a result of comparisons in a program.

var isMarried = true;   // yes, I'm married
var isSingle = false; // no, I'm not single

var a = 2, b = 5, c = 10;

alert(b > a) // Output: true
alert(b > c) // Output: false
Enter fullscreen mode Exit fullscreen mode

The Undefined Data Type

The undefined data type can only have one value, the undefined value. If a variable has been declared, but has not been assigned a value, has the value undefined.

var a;
var b = "Hello World!"

alert(a) // Output: undefined
alert(b) // Output: Hello World!
Enter fullscreen mode Exit fullscreen mode

The Null Data Type

This data type can have only one value as well, the null value. It is not equivalent to an empty string (β€œβ€) or 0, it is simply nothing.

var a = null;
alert(a); // Output: null

var b = "Hello World!"
alert(b); // Output: Hello World!

b = null;
alert(b) // Output: null
Enter fullscreen mode Exit fullscreen mode

Top comments (0)