JavaScript Built-in Data Types

JavaScript is a dynamic and flexible programming language that is widely used for creating interactive web applications. One of the most fundamental concepts in JavaScript is understanding the different built-in data types available. These data types are the building blocks of the language and play a crucial role in determining how your code will behave and interact with different values. In this article, we will be discussing the eight built-in data types in JavaScript: String, Number, Boolean, Array, Object, Function, Undefined, and Null.

  1. String: A sequence of characters used to represent text values.

  2. Number: A numeric value that can be either an integer or a floating-point number.

  3. Boolean: A value that can either be true or false.

  4. Array: An ordered collection of values.

  5. Object: A collection of properties, where each property has a name and a value.

  6. Function: A reusable block of code that can accept inputs and return outputs.

  7. Undefined: A value that represents an absence of a value for a declared variable.

  8. Null: A value that represents an intentional absence of any object value.

With Code Snippets...

// String
var name = "John Doe"; // A string is a sequence of characters, you can use single or double quotes to define a string in JavaScript

// Number
var age = 30; // A number is a numeric value, there is only one type of number in JavaScript and it can be either an integer or a floating-point value
var price = 19.99; // A floating-point value

// Boolean
var isStudent = true; // A boolean is a value that can either be true or false
var isValid = false;

// Array
var fruits = ["apple", "banana", "cherry"]; // An array is an ordered collection of values
console.log(fruits[0]); // Output: apple

// Object
var person = {
  firstName: "John",
  lastName: "Doe",
  age: 30
}; // An object is a collection of properties, where each property has a name and a value
console.log(person.firstName); // Output: John

// Function
function sayHello(name) { // A function is a reusable block of code that can accept inputs and return outputs
  console.log("Hello, " + name);
}
sayHello("John"); // Output: Hello, John

// Undefined
var x; // A variable that has been declared, but has not been assigned a value, has the value 'undefined'
console.log(x); // Output: undefined

// Null
var y = null; // The 'null' value represents an intentional absence of any object value
console.log(y); // Output: null