DEV Community

Cover image for Understanding Data Types in JavaScript ( Part - 2 )
Mohammad Moiz Ali
Mohammad Moiz Ali

Posted on • Originally published at makstyle119.Medium

Understanding Data Types in JavaScript ( Part - 2 )

In addition to string, number, bigint, and boolean, JavaScript has four other built-in data types: undefined, null, symbol, and object. In this blog post, we will discuss each of these data types in detail and provide examples of how to work with them in JavaScript.

Undefined:

The undefined data type represents a value that is not defined. When a variable is declared but not assigned a value, its value is undefined. For example:

let x;
console.log(x); // logs undefined
Enter fullscreen mode Exit fullscreen mode

In this example, the variable x is declared but not assigned a value, so its value is undefined. The console.log statement logs the value of x, which is undefined.

Null:

The null data type represents a deliberate non-value. It is often used to indicate the absence of an object or value. For example:

let y = null;
console.log(y); // logs null
Enter fullscreen mode Exit fullscreen mode

In this example, the variable y is assigned the value null, which represents a deliberate non-value. The console.log statement logs the value of y, which is null.

Symbol:

The symbol data type represents a unique identifier. Symbols are often used to create private object properties and to avoid naming collisions. Symbols are created using the Symbol() function. For example:

const mySymbol = Symbol('My symbol');
console.log(mySymbol); // logs Symbol(My symbol)
Enter fullscreen mode Exit fullscreen mode

In this example, the Symbol() function is used to create a new symbol with the description "My symbol". The console.log statement logs the value of mySymbol, which is the newly created symbol.

Object:

The object data type represents a collection of properties. Objects are used to store and manipulate complex data. Objects are created using the {} or new Object() syntax. For example:

const person = {
  firstName: 'John',
  lastName: 'Doe',
  age: 30
};
console.log(person); // logs {firstName: "John", lastName: "Doe", age: 30}
Enter fullscreen mode Exit fullscreen mode

In this example, the person object is created with three properties: firstName, lastName, and age. The console.log statement logs the value of person, which is the entire object.

Conclusion:

Understanding the different data types in JavaScript is crucial for building robust and efficient programs. Undefined, null, symbol, and object are four additional built-in data types in JavaScript. By understanding how to work with these data types, you can write more effective and efficient code in your JavaScript programs.

Top comments (0)