DeveloperBreeze

Primitive Data Types

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

Related Posts

More content you might like

Tutorial
javascript

Comparison and Logical Operators

// AND operator
console.log(true && true); // true
console.log(true && false); // false

// OR operator
console.log(false || true); // true
console.log(false || false); // false

// NOT operator
console.log(!true); // false
console.log(!false); // true

Use comparison and logical operators together for complex conditions.

Dec 11, 2024
Read More
Tutorial
javascript

Arithmetic Operators

     let power = 2 ** 3; // 8
  • Increases a variable by one.
  • Postfix Increment (i++):

Dec 11, 2024
Read More
Tutorial
javascript

Non-Primitive Data Types (Objects, Arrays, and Functions)

  fruits.push("orange");
  console.log(fruits); // ["apple", "banana", "cherry", "orange"]
  • Removing Elements:

Dec 11, 2024
Read More
Tutorial
javascript

Variables and Constants

let greeting;
console.log(greeting); // undefined
  • Use let for variables whose value may change.
  • Example:

Dec 10, 2024
Read More

Discussion 0

Please sign in to join the discussion.

No comments yet. Be the first to share your thoughts!