JavaScript variables and data types
Tags: javascript
Published: 2020-03-29 17:06:28

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.
```javascript
let message = "Hello World"
message = 1234;
``` 
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).
```javascript
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
``` 
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.
```javascript
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
```

###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.
```javascript
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
```

###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.
```javascript
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
```
###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_.
```javascript
var a;
var b = "Hello World!"
 
alert(a) // Output: undefined
alert(b) // Output: Hello World!
```
###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.
```javascript
var a = null;
alert(a); // Output: null
 
var b = "Hello World!"
alert(b); // Output: Hello World!
 
b = null;
alert(b) // Output: null
```