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

Comparison and logical operators allow you to compare values and perform logical operations. They are fundamental for decision-making in JavaScript, such as in if statements and loops.

Comparison operators compare two values and return a boolean (true or false).

Dec 11, 2024
Read More
Tutorial
javascript

Arithmetic Operators

   for (let i = 0; i < 5; i++) {
     console.log("Count:", i);
   }
   // Outputs:
   // Count: 0
   // Count: 1
   // Count: 2
   // Count: 3
   // Count: 4
  • Integer Division:
  • Unlike some languages, dividing integers in JavaScript returns a floating-point number.

Dec 11, 2024
Read More
Tutorial
javascript

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

  • Deleting Properties:
  delete person.isStudent;
  console.log(person);

Dec 11, 2024
Read More
Tutorial
javascript

Variables and Constants

     function test() {
       var y = 20;
       console.log(y); // 20
     }
     console.log(y); // Error: y is not defined
let name = "Alice";
console.log(name);

const BIRTH_YEAR = 1990;
// BIRTH_YEAR = 2000; // Error: Assignment to constant variable

Dec 10, 2024
Read More

Discussion 0

Please sign in to join the discussion.

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