Javascript Types

JavaScript Types

JavaScript is a dynamically-typed language, which means that variables can hold values of different types. In JavaScript, there are several built-in types that we can use to define and manipulate data. Understanding the different types in JavaScript is essential for writing correct and efficient code.

1. Primitive Types

In JavaScript, there are six primitive types:

String: Represents a sequence of characters. Strings in JavaScript are immutable, which means that once a string is created, it cannot be changed.

const greeting = "Hello, World!";
const name = 'Alice';

Number: Represents numeric values. JavaScript uses a single Number type to represent both integers and floating-point numbers.

const age = 30;
const price = 9.99;

Boolean: Represents a logical entity and can have two values: `true` or `false`.

const isLogged = true;
const hasPermission = false;

Undefined: Represents the absence of a value. Variables that are declared but not assigned a value are automatically set to `undefined`.

let username;
console.log(username); // Output: undefined

Null: Represents the intentional absence of any value. It's often used to indicate the absence of an object or value. It is usually assigned to variables as a way to clear their contents.

let person = null;
console.log(person); // Output: null

Symbol: Represents a unique and immutable value, often used as object property keys to avoid naming collisions.

const id = Symbol('id');
const obj = {
  name: 'John',
  [id]: 123
};

2. Object Type

The object in JavaScript is a complex data type that can hold multiple values in key-value pairs. Objects are composed of properties, where each property consists of a key and a value. The value of an object's property can be of any type.

const person = {
  name: 'Alice',
  age: 30,
  isStudent: true
};

3. Array Type

An array is a special type of object that is used to store multiple values in a single variable. Each value in an array is called an element, and it is identified by an index. Arrays can hold elements of different types.

const fruits = ['apple', 'banana', 'orange'];
const numbers = [1, 2, 3, 4, 5];
const mixed = ['John', 30, true, null];

4. Function Type

In JavaScript, functions are first-class objects, which means that they can be assigned to variables, passed as arguments to other functions, and returned as values from functions. Functions can be defined using function declarations or function expressions.

function sayHello(name) {
  console.log(`Hello, ${name}!`);
}
 
const add = function(a, b) {
  return a + b;
};

5. Type Conversion

JavaScript provides several built-in functions to convert values from one type to another. These functions include:

  • String(): Converts a value to a string.

  • Number(): Converts a value to a number.

  • Boolean(): Converts a value to a boolean.

  • parseInt(): Converts a string to an integer.

  • parseFloat(): Converts a string to a floating-point number.

const num = 10;
const str = String(num);
 
console.log(typeof num); // Output: number
console.log(typeof str); // Output: string

Key Takeaways

  • JavaScript has several built-in types: string, number, boolean, undefined, null, and symbol.

  • Objects in JavaScript are used to hold multiple values in key-value pairs.

  • Arrays are a special type of object used to store multiple values in a single variable.

  • Functions in JavaScript are first-class objects and can be assigned to variables, passed as arguments, and returned as values.

  • JavaScript provides type conversion functions to convert values from one type to another.

Comments

Popular posts from this blog

Limitations of JavaScript

'for in' Loop

in Operator