Basic Data Types

Understanding data types is fundamental for doing anything substantial with JavaScript. Here, we will dive into the basic data types supported by JavaScript, using concise explanations and practical code examples to make things clear and to-the-point.

Number

JavaScript includes both integer and floating-point numbers under a single data type called Number.

Example:

let length = 16; // Integer let weight = 7.5; // Floating-point

JavaScript also uses exponential notation for very large or very small numbers.

let largeNumber = 123e5; // 12300000 let smallNumber = 123e-5; // 0.00123

BigInt

For numbers larger than Number can handle, JavaScript includes the BigInt type.

Example:

let bigNum = BigInt("123456789012345678901234567890");

String

Strings are sequences of characters used to represent text. They can be enclosed in single or double quotes.

Example:

let color = "Yellow"; let lastName = 'Johnson';

You can embed quotes within strings by using opposite types of quotes or escaping quotes with a backslash.

let sentence1 = "It's a sunny day."; let sentence2 = 'She said, "Hello!"';

Boolean

The Boolean data type can only have two values: true or false. This is often used in conditional testing.

Example:

let isTrue = true; let isFalse = false;

Booleans are commonly used in comparisons:

let x = 5; let y = 5; let z = 6; console.log(x == y); // true console.log(x == z); // false

Undefined

A variable that has been declared but not assigned a value will have the value undefined.

Example:

let car; console.log(car); // undefined

Null

null represents the intentional absence of any object value. It is often used to reset a variable.

Example:

let house = null;

Symbol

Symbols are new and unique primitive values, introduced in ES6.

Example:

let sym1 = Symbol(); let sym2 = Symbol('description'); console.log(sym1 === sym2); // false, because symbols are unique

Object

Objects are collections of properties. Each property is defined as a key-value pair.

Example:

let person = { firstName: "John", lastName: "Doe", age: 30 };

Objects can also be arrays:

let fruits = ["Apple", "Banana", "Cherry"];

Nested Structures

Objects can contain other objects and arrays:

let complexObject = { name: "Building", floors: [ { level: 1, occupants: 10 }, { level: 2, occupants: 15 } ] };

**Type Checking with **typeof

You can use the typeof operator to check the type of a variable or an expression.

Example:

console.log(typeof "Hello"); // string console.log(typeof 123); // number console.log(typeof true); // boolean console.log(typeof undefined); // undefined console.log(typeof {a:1}); // object console.log(typeof Symbol()); // symbol console.log(typeof null); // object (this is a quirk in JavaScript)

Dynamic Typing

JavaScript is dynamically typed, meaning you can reassign variable values of different types.

Example:

let x; // Now x is undefined x = 5; // Now x is a Number x = "John"; // Now x is a String

Practical Examples

Mixing Numbers and Strings

When a number is added to a string, JavaScript converts the number to a string and concatenates the values.

Example:

let result = 5 + "Apples"; console.log(result); // "5Apples"

Sequence matters with concatenation:

let result1 = 5 + 5 + "Apples"; console.log(result1); // "10Apples" let result2 = "Apples" + 5 + 5; console.log(result2); // "Apples55"

By understanding these basic data types and their behavior, you can begin to structure your JavaScript code more effectively, creating more reliable and bug-free applications. Keep this reference handy as you explore more advanced topics in JavaScript!

Start Building in JavaScript Today

Lilbots is the best platform for creating powerful JavaScript bots

  • Built in access to cutting edge AI models
  • Out of the box integration with hundreds of APIs
  • Collaborate with coding workspaces
  • Discover and fork thousands of bots built by the community

Learn JavaScript

JavaScript Objects

JavaScript Arrays

JavaScript Basics