DEV Community

Cover image for What are data types and scope in javascript??
Mandeep Singhmar
Mandeep Singhmar

Posted on • Edited on

What are data types and scope in javascript??

What are Data Types in javascript?

Data types basically specify what kind of data can be stored and manipulated in javascript.

There are two types of data types: primitive type which referred as value types and object type referred as reference types.

Primitive types:

There are 7 primitive types in javascript
Number, string, boolean, symbol, undefined and null.

Why i am saying primitive types to value types, bcz primitive data types copied by their value.

Let's take a look at example:


let a = 5;
let b = a;
a = 10;

console.log(a) // 10
console.log(b) // 5 

Enter fullscreen mode Exit fullscreen mode

Object types

Objects, function and array are object types and can call be reference types.

And objects are copied by their reference not their value.

Let's take a look at example:


let a = {value:5};
let b = a;
a.value = 10;

console.log(a) // {value:10}
console.log(b) // {value:10} 

Enter fullscreen mode Exit fullscreen mode

Have you notice we have used a keyword name let, what it is??
let is keyword that allows us to declare the variables.

Variable

Now again, what is variable??

let's take a look at variable:
variable is a name or identifier which is used to refer a value. A variable is a combination of letters, numbers, special characters and dollar sign with the first letter being the alphabet.
And also we can't use reserved keyword like let,var,const, this and much more.

Scope

scope means where you can access the specific variables and functions in our code and a variable defined inside a scope is accessible only within that scope, but inaccessible outside. In JavaScript, scopes are created by code blocks, functions, modules.

Top comments (0)