Sign in
Log inSign up

Basics of JS (II)

Prathamesh Parab
·May 31, 2021·

4 min read

Basics of JS (II)

Hoisting:

Hoisting is a mechanism where variables and function declarations are moved to the top of their scope before code execution.

console.log(x); // prints undefined
var x = 100;
console.log(x); //prints 100
  • Variables declared using var are hoisted to the top of their scope and initialized with a value of undefined(special type).
  • Variables declared using let are hoisted to the top of their scope but are not initialized with any value.
  • Variables declared using const are hoisted to the top of their scope but are not initialized with any value.
console.log(x); // prints undefined
var x = 100;
console.log(x); //prints 100

console.log(y); //Reference error
let y = 200;
console.log(y); //prints 200

console.log(z); //Reference error
const z = 300;
console.log(z); //prints 300

What are Datatypes?

Data types are a crucial notion in programming.

It is necessary to have some knowledge about the type in order to act on variables.

For example, 2 and 3 are numbers, and when added together, they equal 5.

JavaScript is a dynamic, loosely typed language. Variables in JavaScript are not tied to any specific value type, and each variable can be assigned (and reassigned) values of any kind.

In Javascript, there are 7 primitive datatypes:

  • Boolean
  • Number
  • String
  • Null
  • Undefined
  • Bigint
  • Symbol

Boolean

Boolean represents a logical entity and can have two values: true and false.

var a = true //Variable 'a' holds value of boolean type.

Number

The number type represents both integer and floating point numbers (decimals and exponentials).

A variable of number type can range between [-(253 − 1),(253 − 1)].

In addition to representing floating-point numbers, the number type has three symbolic values: +Infinity, -Infinity, and NaN (“Not a Number”).

var a = 2.3   //Variable 'a' holds value of number type.

var b = 2     //Variable 'b' holds value of number type.

String

String type is used to represent textual data. A string in JavaScript must be surrounded by quotes(double, single or backticks). It is a set of elements and each element in the string occupies a position.

var a = "Hello";  //Variable 'a' holds value of string type

Null

A variable that has been declared but not been assigned a value has the value undefined.

var a;  //Variable 'a' holds undefined

var b = undefined;  //Variable 'a' holds undefined

BigInt

BigInt type has been recently added to the language to represent integers of arbitrary length (numbers which cannot be represented in number type.

A BigInt value is created by appending n to the end of an integer. For e.g, 23n is a value of bigint type.

var a = 9324012456789012613321123n;

Symbol

A Symbol is a unique and immutable primitive value and may be used as the key of an Object property. It is used to create unique identifiers for objects.

More of Datatypes

Object:

Object refers to a data structure containing data and instructions for working with the data. Objects sometimes refer to real-world things, for e.g, a vehicle etc.

Objects can contain many values in form of key-value pairs.

JavaScript objects are written with curly braces {}.

var a = {    //Variable 'a' stores an object which contains several key-value pairs.
    name: "Prathameshj",
    age: 20
}

Function

A function is a code snippet that can be called by other code or by itself, or a variable that refers to the function.

var a = function () {   //Variable 'a' holds a function
    console.log("Hello world!");
};

typeof Keyword

typeof is an operator which can easily help us to find the type of a certain variable.

For e.g console.log(typeof 30) prints number as output, signifying that the value 30 belongs to number type.

Type Conversion

JavaScript variables can be converted to a new variable and another data type by the use of a JavaScript function.

var a = true;
var b = String(true);
console.log(typeof a);   //prints boolean  
console.log(typeof b);  //prints string