DEV Community

Himanshu Gupta
Himanshu Gupta

Posted on

Understanding Data Types in JavaScript

In JavaScript, data types play a crucial role in defining the nature of variables and how they behave in our code. Broadly, JavaScript data types can be classified into two categories: Primitive and Non-Primitive (also known as Reference or Object) data types.

Primitive Data Types
Primitive data types are immutable and hold primitive values directly. They are the basic building blocks of data manipulation in JavaScript. Let's take a closer look at each primitive data type along with its typeof output:

Image description

  1. Number: Represents numeric values.
  2. String: Represents textual data.
  3. Boolean: Represents true or false values.
  4. BigInt: Represents integers with arbitrary precision.
  5. Symbol: Represents unique identifiers.
  6. Null: Represents the absence of any value.
  7. Undefined: Represents a variable that has been declared but has not been assigned a value.

Non-Primitive Data Types
Non-primitive data types are mutable and store references to memory locations. They are more complex data structures and include objects, arrays, and functions.

Image description

  1. Object: Represents a collection of key-value pairs. 2, Array: Represents a collection of elements arranged in an ordered list. 3, Function: Represents executable code blocks that can be invoked.

Top comments (2)

Collapse
 
efpage profile image
Eckehard • Edited

Javascript is a dynamically typed language, so a variable can contain anything. The more important it is to have working type checks. Here is a list of functions I am using. I was happy to get some additions/corrections on this:

  isArr = Array.isArray
  isEle = (c) => c instanceof Element
  isNode = (c) => c instanceof Node
  isStr = (s) => typeof s === 'string'
  isObj = (c) => typeof c === 'object' && !isArr(c) && c !== null
  isDef = (c) => typeof c !== "undefined";
  isNum = (c) => typeof c === "number";
  isFnc = (c) => typeof c === "function";
  isEmpty = (s) => s.length === 0
  isBlank = (s) => s.trim().length === 0
Enter fullscreen mode Exit fullscreen mode
Collapse
 
himanshudevgupta profile image
Himanshu Gupta

@efpage Thankyou for update