Primitive Data Types

JavaScript has six primitive data types: string, number, boolean, null, undefined, and symbol. These types are the building blocks of your code and understanding them is essential for mastering JavaScript.

String

A string represents textual data. Strings are created by enclosing the text in single quotes ('), double quotes ("), or backticks (`).

let username = 'HunterMacias';
let greeting = "Hello, world!";
let templateLiteral = `Hello, ${username}!`;

Strings can also include special characters like new lines (\n), tabs (\t), and Unicode characters (\u).

let multiline = `This is a
multiline string.`;
console.log(multiline); // This is a
                        // multiline string.

Number

A number represents both integer and floating-point values.

let age = 30; // Integer
let pi = 3.14159; // Float
let hex = 0xff;  // Hexadecimal
let binary = 0b1010;  // Binary
let octal = 0o17;  // Octal

Numbers also include special values like Infinity, -Infinity, and NaN (Not-a-Number).

Quirk

JavaScript uses 64-bit floating-point representation, which can lead to precision issues:

console.log(0.1 + 0.2); // 0.30000000000000004

Boolean

A boolean represents a logical entity and can have two values: true or false.

let isLoggedIn = true;
let isPremiumUser = false;

Booleans are often used in conditional statements to control the flow of a program.

if (isLoggedIn) {
  console.log('User is logged in.');
} else {
  console.log('User is not logged in.');
}

Null

null is a special value that represents "no value" or "nothing".

let selectedProduct = null;

Null is often used to indicate that a variable should have an object or value, but that it is not currently available.

Undefined

Undefined means a variable has been declared but not yet assigned a value.

let customerName;
console.log(customerName); // undefined
Quirk

If you try to access an object property that doesn't exist, you'll get undefined.

let user = { name: 'HunterMacias' };
console.log(user.age); // undefined
Example

Let's explore a more detailed example to understand how undefined works in different scenarios:

// Variable declared but not initialized
let product;
console.log(product); // undefined

// Function with no return statement
function greet() {
  let message = "Hello!";
}
let greeting = greet();
console.log(greeting); // undefined

// Accessing a non-existent property of an object
let userProfile = {
  username: 'HunterMacias',
  email: 'hunter@example.com'
};
console.log(userProfile.age); // undefined

Symbol

Symbol is a unique and immutable primitive value, often used as object keys.

let uniqueId = Symbol('id');

let user = {
  name: 'HunterMacias',
  age: 30,
  isLoggedIn: true,
  address: null,
  id: Symbol('id')
};

Symbols are primarily used to create unique property keys that won't conflict with other property keys.

Understanding these primitive data types is crucial for working effectively in JavaScript. They form the basis of more complex data structures and operations.

FAQ

Q: Can I use single and double quotes interchangeably?

A: Yes, but be consistent to avoid confusion. Backticks are useful for template literals and multi-line strings.

Q: Why does `0.1 + 0.2` not equal `0.3` exactly?

A: This is due to the way JavaScript handles floating-point arithmetic. It's a common issue with many programming languages that use binary floating-point numbers.

Q: How is `null` different from `undefined`?

A: `null` is an assignment value that means "no value" or "nothing", while `undefined` means a variable has been declared but not yet assigned a value.

Q: Why is understanding primitive data types important?

A: Knowing how these data types work helps you write more efficient and bug-free code. Each type has specific properties and behaviors that can impact how your programs run.