Sign in
Log inSign up

data types of javascript

javascript datatypes

Ankitkumar Ravidas's photo
Ankitkumar Ravidas
·Apr 11, 2021·

3 min read

Javascript datatypes :

  • To use any data its imp to know type of that data
  • JavaScript has dynamic types of data. means that the same variable can be used to hold different data types: var x = 12 ; var x = “ankit” ;

  • Javascript is a dynamic typing language, Javascript engine (chrome - v8, IE - chakra, Firefox - Spidermonkey.) automatically decides what type of variable is based on the value assigned to it.

Coercion :

  • Coercion is to convert one type to another with or without your awareness. Coercion happens when operands are of different types:
  • For instance: 1+ ‘2’ , returns text ’12’. Javascript converts number 1 to string ‘1’ and concat with string ‘2’.

Data Types :

  1. Null
  2. Undefined
  3. String
  4. Number
  5. Boolean
  6. Object
  7. Array
  8. Function

“typeof” keyword is used to get type of any data/variable

Ex : var a = true; console.log(typeof a);

5) boolean :

  • always represent a logical entity
  • Only has two values (true and false)

Ex : var a = true; console.log(typeof a); => boolean

1) Null :

  • Null only contains one value null
  • Datatype of null is object
  • Until you do not specifically assign null to any variable Variable will not be null

Ex : var b = null; console.log("b", typeof b); => object

2) undefined :

  • Variable which does not have value called as undefined values
  • In JavaScript, a variable without a value, has the value undefined. The type is also undefined.

undefined and null are equal in value but different in type: typeof undefined => undefined typeof null => object

console.log(null === undefined) => false console.log(null == undefined) => true

4) number :

  • Largest javascript number is +infinity and smallest in -infinity
  • If you divide any number(positive/negative) with 0 you will get (+infinity/-infinity)
  • NAN( not a number) - shows you a some computational error, ex : if you devices nun number t number gives error of NAN it is result of incorrect mathematical error, if you get this start checking datatype of variables

Ex : var x = 12;

3) string : represents a textuals data

  • Index always start with 0
  • every character contains a position and we can access it by position number

Ex : var x = “chrome”;

6) object :

  • always contain keys and values,
  • JavaScript objects are written with curly braces {}.
  • Object are written as name:value pairs, separated by commas.
  • Mostly property:value of single thing(1 car , 1 student ) Ex : var employee = {name : ”ankit” , age : 26, gender : “male”}

7) array :

  • contains only value/bunch of value,
  • Mostly contains value of multiple object(ex : names of 100 students, ) Ex var empName = [“ankit”,”rakesh”,”manoj”];
  • Array items are separated by commas.
  • Array indexes are zero-based
  • JavaScript arrays are written with square brackets [].

8) function :

  • contain commands/single job to do
  • Type of function is function

#javascriptdatatypes

Hassle-free blogging platform that developers and teams love.
  • Docs by Hashnode
    New
  • Blogs
  • AI Markdown Editor
  • GraphQL APIs
  • Open source Starter-kit

© Hashnode 2024 — LinearBytes Inc.

Privacy PolicyTermsCode of Conduct