Published on December 11, 2024By DeveloperBreeze

Section 2.2: Data Types

Tutorial 2.2.1: Primitive Data Types


Primitive Data Types in JavaScript

JavaScript provides several primitive data types to store basic values. These data types are immutable, meaning their values cannot be changed directly. Understanding these types is fundamental for working with data in JavaScript.


What are Primitive Data Types?

Primitive data types represent single values (as opposed to complex objects). JavaScript has the following primitive types:

  1. String
  2. Number
  3. BigInt
  4. Boolean
  5. Undefined
  6. Null
  7. Symbol

1. String

A string is used to represent text.

  • Syntax: Strings can be enclosed in single ('), double ("), or backticks (` ``).
  • Examples:
  let name = "Alice";
  let greeting = 'Hello';
  let message = `Welcome, ${name}!`; // Template literal
  console.log(message); // "Welcome, Alice!"

2. Number

The number type represents both integers and floating-point numbers.

  • Examples:
  let age = 30; // Integer
  let price = 19.99; // Float
  console.log(age + price); // 49.99
  • Special values:
  • Infinity: Result of dividing by zero.
    console.log(10 / 0); // Infinity
  • NaN: Result of an invalid number operation.
    console.log("abc" * 2); // NaN

3. BigInt

Used for very large integers beyond the safe range of number.

  • Syntax: Append n to the end of an integer.
  • Example:
  let bigNumber = 123456789012345678901234567890n;
  console.log(bigNumber);

4. Boolean

Represents true or false.

  • Examples:
  let isLoggedIn = true;
  let hasAccess = false;
  console.log(isLoggedIn && hasAccess); // false

5. Undefined

A variable that has been declared but not assigned a value is undefined.

  • Example:
  let user;
  console.log(user); // undefined

6. Null

The null value represents an intentional absence of any object value.

  • Example:
  let currentUser = null;
  console.log(currentUser); // null

7. Symbol

Symbols are unique identifiers.

  • Example:
  let id = Symbol("id");
  console.log(id); // Symbol(id)

Type Checking

Use the typeof operator to check the type of a value.

  • Examples:
  console.log(typeof "Hello"); // string
  console.log(typeof 42); // number
  console.log(typeof null); // object (this is a historical quirk)
  console.log(typeof undefined); // undefined

Exercise

  1. Declare variables for the following:
  • Your name (string).
  • Your age (number).
  • Whether you are a student (boolean).
  1. Log their types using typeof.

Example:

let name = "Alice";
let age = 25;
let isStudent = true;

console.log(typeof name); // string
console.log(typeof age); // number
console.log(typeof isStudent); // boolean

Comments

Please log in to leave a comment.

Continue Reading:

Generate Random Password

Published on January 26, 2024

javascriptpythonphp

JavaScript Promise Example

Published on January 26, 2024

php

JavaScript Word Count in a Sentence

Published on January 26, 2024

javascript

Building a Real-Time Chat Application with WebSockets in Node.js

Published on August 03, 2024

javascriptcsshtml

JavaScript Code Snippet: Fetch and Display Data from an API

Published on August 04, 2024

javascriptjson

Building a Modern Web Application with React and Redux

Published on August 05, 2024

javascript

Advanced TypeScript: Type Inference and Advanced Types

Published on August 05, 2024

typescript

Building Progressive Web Apps (PWAs) with Modern APIs

Published on August 05, 2024

jsonbash