JavaScript Course | Data Types in JavaScript - GeeksforGeeks

29 Apr, 2019
Previous article: JavaScript Course | Variables in JavaScript
Datatypes in JavaScript
There are majorly two types of languages. First, one is Statically typed language where each variable and expression type is already known at compile time. Once a variable is declared to be of a certain data type, it cannot hold values of other data types.Example: C, C++, Java.
// Java(Statically typed)// variable x is of type int and it// will not store any other type.int x = 5;  // type string and will only accept string valuesstring y = 'abc'; 
Other, Dynamically typed languages: These languages can receive different data types over time. For example- Ruby, Python, JavaScript etc.
// Javascript(Dynamically typed)var x = 5; // can store an integervar name = 'string'; // can also store a string.
JavaScript is dynamically typed (also called loosely typed) scripting language. That is, in javascript variables can receive different data types over time. Datatypes are basically typed of data that can be used and manipulated in a program. A variable in JavaScript can contain any data. This means that a variable at one time can be a number and at another be a string.
The latest ECMAScript(ES6) standard defines seven data types: Out of which six data types are Primitive(predefined).
Numbers: 5, 6.5, 7 etc.
String: “Hello GeeksforGeeks” etc.
Boolean: Represent a logical entity and can have two values: true or false.
Null: This type has only one value : null.
Undefined: A variable that has not been assigned a value is undefined.
Object: It is the most important data-type and forms the building blocks for modern JavaScript. We will learn about these data types in details in further articles.
A numberThe number type in javascript contains both integer and floating point numbers. Besides these numbers, we also have some ‘special-numbers’ in javascript that are: ‘Infinity’, ‘-Infinity’ and ‘NaN’. The Infinity basically represents the mathematical ‘?’. The ‘NaN’ denotes a computational error.
let num = 2; // integer 
let num2 = 1.3; // floating point number
let num3 = Infinity; // Infinity
let num4 = 'something here too'/2; // NaN

StringA String in javascript is basically a series of characters that are surrounded by quotes. There are three types of quotes in Javascript, which are:
let str = "Hello There";
let str2 = 'Single quotes works fine';
let phrase = `can embed ${str}`;

There’s no difference between ‘single’ and “double” quotes in javascript. Backticks provide extra functionality as with the help of them we can embed variables inside them.
let name = "Mukul";

// embed a variable
alert( `Hello, ${name}!` ); // Hello, Mukul!

A BooleanThe boolean type has only two values: true and false.
This data type is used to store yes/no values: true means “yes, correct”, and false means “no, incorrect”.
 let isCoding = true; // yes
 let isOld = false; // no

A nullThe special null value does not belong to any of the default data types. It forms a separate type of its own which contains only the null value:
let age = null;
The ‘null’ data type basically defines a special value which represents ‘nothing’, ’empty’ or ‘value unknown’.
UndefinedJust like null, Undefined makes its own type. The meaning of undefined is ‘value is not assigned’.
let x;
console.log(x); // undefined

ObjectObjects are not primitive in nature and a bit complex to understand. Everything in javascript is basically an object, and that is the reason why it becomes very important to have a good understanding of what they are. Objects are used to store keyed collections of various data and more complex entities.We can create objects in multiple ways. One is by making use of figure brackets {...} with an optional list of properties. The properties of an object are in the form of ‘key: value’ pair. Another way is to make use of the ‘new’ keyword.An empty Object can be created using given below syntax.
let person = new Object(); // "object constructor" syntax
let person = {};  // "object literal" syntax

Both these method are correct, though it’s totally your call what to choose. We can also put properties inside an Object like:
<script>// an objectlet person = {       name: "Mukul",  // by key "name" store value "Mukul"  age: 22        // by key "age" store value 22};</script>
Next article: JavaScript Course | Operators in JavaScript
aayushimittal3
JavaScript-Course
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Remove elements from a JavaScript Array
Convert a string to an integer in JavaScript
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
How to append HTML code to a div using JavaScript ?
Remove elements from a JavaScript Array
Installation of Node.js on Linux
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?