Variables and Primitive Types

Variables and Primitive Data Types

Variables:

Variable means anything that can vary. JavaScript includes variables which hold the data value and it can be changed anytime.

JavaScript uses reserved keyword var to declare a variable.

Example:

var x; // declaring a variable as x.

If you console log variable x, it will give output as undefined because no value is assigned to a variable x.

var x;
console.log(x); // Prints: undefined

You can assign a value to a variable using equal to (=) operator when you declare it or before using it.

Example:
var x = 5; // assigning value to a variable
console.log(x); // Prints: 5

If a variable is neither declared nor defined then we try to reference such variable, the result will be not defined.

Example:
var x = 5;
console.log(age); // ReferenceError: age is not defined

Rules for naming variables:

  1. Variable name can have digits, letters, $ symbol, underscore.
  2. Must start with letter or $ symbol or underscore.
  3. Variable name should not be a keyword.
  4. Variable name can not start with a number.

There are other variable keywords introduced in ES6. They are

  1. let - let keyword to define a variable with restricted scope.
  2. const - const keyword to define a variable that cannot be reassigned.

Primitive Data Types:

Primitive data types are stored as simple data types.

Primitive data types are immutable.

There are 6 primitive data types:

  1. string
  2. number
  3. boolean
  4. null
  5. undefined
  6. symbol()

string:

The sequence of characters delimited by either single or double-quotes.

Example:
var car = "Audi";
console.log(typeof car); // Prints: string

number:

Any integer or floating-point numeric value.

Example:
var model = 2018;
console.log(typeof model); // Prints: number

boolean:

Which tells true or false

Example:
var available = true;
console.log(typeof available); // Prints: boolean

null:

This is special primitive-type that has only one value i.e., null.

Example:
var owner = null;
console.log(typeof owner);
 // Prints: object

In above example, as you can see it printed as object instead of null because it is a bug in javascript. It’s there since begining of javascript.

undefined:

A primitive type that had only value, undefined. The undefined is the value assigned to a variable that wasn’t initialized.

Example:
var condition = undefined;
console.log(typeof condition); // Prints: undefined

symbol( ):

A unique and unaltered value.

Example:
var vol = Symbol();
console.log(typeof vol); // Prints: symbol

Coercion

The way to convert from one data type to another(such as string to number, object to boolean, and so on) is called coercion.

Example:
var value1 = "5";
var value2 = 9;
var sum = value1 + value2;

console.log(sum); //Output: 59

In above example, JavaScript has coerced the 9 from a number into a string and then concatenated the two values together, resulting in a string of 59.

String Concatenation

The same + operator you use for adding two numbers can be used to concatenate two strings.

Example:
var msg1 = "There are ";
var numStudents = 25;
var msg2 = " students in class";

console.log(msg1 + numStudents + msg2); //Output:  "There are 25 students"

Referential Data Types (non-primitive)

  1. Functions
  2. Arrays
  3. Objects