JavaScript Data Types

Variables are named containers that can store data and Type is a classification identifying one of various types of data. Data can come in many different types

Strongly Typed

JavaScript Data Types

Normally most of the programming languages feature strongly typed variables. When programming with these languages, we should explicitly state what sort of data we are dealing with, and use of those data should follow strict rules applicable to its datatype. For example, we can't add a numeric and a string together in Strongly Typed languages.

Weakly Typed

On the other hand, Javascript variables are loosely typed, meaning that a variable can hold any type of data. When we deal with data in Javascript, we often don't need to specify type of data, instead Javascript will work that out for itself. Moreover, when you are using different types of data at the same time, JavaScript will work out behind the scenes what you're trying to do

Javascript Data structures

Even though Javascript is loosely typed language, it actually does have data types (also called primitive types). Normally there are three Primary data types and two Composite data types and Two special Data Types in Javascript

Primary Data Types (Primitive)

String

Number

Boolean

Composite Data Types (reference)

Object

Array

Special Data Types

Null

Undefined

String : A strings of any characters enclosed in quotes is a string primitive.

Number : A number by itself is a number primitive, all numbers in JavaScript are 64-bit floating-point numbers.

Boolean : It will store Variables or expressions which are either true or false.

Object: A collections of properties and methods. In Javascript, anything that is not a primitive is an Object.

Arrays : Arrays are regular objects, both objects and arrays can have properties and methods.

Null : It has only one value in JavaScript: null.

Undefined : It treated as a variable that has been declared, but has never had a value assigned to it.

Var in Javascript

JavaScript var Data Types

Before you can use a variable, you should know what is var declaration in Javascript. Variables in Javascript are declared with the var keyword. The var keyword inform the computer that it needs to reserve some memory for your data to be stored in later. To declare a variable as var type , use the var operator followed by the variable name.

var anyData;

The above statement declare a variable in var type, since var is a dynamic types, it can be use to store any type of data such as numeric, string etc.

var anyData; anyData = 'Hello'; //string assignment

In the above statement anyData hold a string value, string can used inside singles quotes and double quotes.

var anyData; anyData = 5678; //numeric assignment

In the above statement anyData hold a numeric value;

var anyData; anyData = true; //boolean assignment

In the above statement anyData hold a Boolean value;

Assigning Var variables with the Value of Other Variables

var fisrtVar; var secondVar; fisrtVar = 100; secondVar = fisrtVar;

Combining Data Types

var xyz = 10 + 20 + 'is the result';

output is 30 is the result

but..

var xyz = 'result is' + 10 + 20;

output is result is 1020

Scope of JavaScript Variables

Scope of JavaScript Variables

The Javascript variable has only two scopes, Global and Local. When you declare a variable within your function , its scope is local, that means you can use that variable only inside that function. If a variable declare outside the function, its scope is Global. That means you can use this variable to any other code in the current document

Constants

In Javascript you can define read-only Constant variables with const keyword.

const pie = '3.14';

JavaScript keywords

The following are reserved keywords in JavaScript

Javascript keywords