Practice
Resources
Contests
Online IDE
New
Free Mock
Events New Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
logo
Events
Attend free live masterclass hosted by top tech professionals
New
Scaler
Explore Offerings by SCALER

Datatypes And Type Casting

What are Datatypes?

In programming, data types is an important concept.

To be able to operate on variables, it is important to know something about the type.

For eg, 2 and 3 are numbers and when we add them, we get 5.

JavaScript is a loosely typed and dynamic language. Variables in JavaScript are not directly associated with any particular value type, and any variable can be assigned (and re-assigned) values of all types.

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

The Null type has exactly one value: null. Its a special value which can represent “nothing”, “empty” or “value unknown”.

var a = null   //Variable 'a' holds a value of null type.

 

Undefined

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 'b' 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.

 

Some more 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: "Max",
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

Try the following example in the editor below.

Given an input string which represents a positive number, you have to print the next greater integer.

Sample Input</div>

123

Sample Output</div>

124

Hint

We can the next greater number by using ‘+’ operator. For e.g. console.log(5+1) prints 6.

Start solving Datatypes And Type Casting on Interview Code Editor
Hints
  • Hints are not available for this problem

Discussion


Loading...
Click here to start solving coding interview questions
Free Mock Assessment
Fill up the details for personalised experience.
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Graduation Year *
Graduation Year *
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
*Enter the expected year of graduation if you're student
Current Employer
Company Name
College you graduated from
College/University Name
Job Title
Job Title
Engineering Leadership
Software Development Engineer (Backend)
Software Development Engineer (Frontend)
Software Development Engineer (Full Stack)
Data Scientist
Android Engineer
iOS Engineer
Devops Engineer
Support Engineer
Research Engineer
Engineering Intern
QA Engineer
Co-founder
SDET
Product Manager
Product Designer
Backend Architect
Program Manager
Release Engineer
Security Leadership
Database Administrator
Data Analyst
Data Engineer
Non Coder
Other
Please verify your phone number
Edit
Resend OTP
By clicking on Start Test, I agree to be contacted by Scaler in the future.
Already have an account? Log in
Free Mock Assessment
Instructions from Interviewbit
Start Test