JavaScript

JavaScript Data Types - Numbers, Strings, and Booleans


JavaScript supports various data types that allow you to work with different kinds of values. In this guide, we'll explore three fundamental data types: Numbers, Strings, and Booleans.

Numbers

Numbers in JavaScript can be integers or floating-point values. You can perform various mathematical operations with them. Here's how to work with numbers:

// Declare numbers
let age = 25;
let price = 19.99;
// Perform arithmetic operations
let sum = age + 5;
let product = price * 2;
// Use built-in Math object
let squareRoot = Math.sqrt(25);
console.log(`Age: ` + age);
console.log(`Price: ` + price);
console.log(`Sum: ` + sum);
console.log(`Product: ` + product);
console.log(`Square Root: ` + squareRoot);
    

Strings

Strings are sequences of characters and are commonly used for text. You can manipulate and work with strings in various ways:

// Declare strings
let firstName = `John`;
let lastName = 'Doe';
// Concatenate strings
let fullName = firstName + ` ` + lastName;
// Find the length of a string
let nameLength = fullName.length;
// Access individual characters
let firstChar = fullName[0];
console.log(`First Name: ` + firstName);
console.log(`Last Name: ` + lastName);
console.log(`Full Name: ` + fullName);
console.log(`Name Length: ` + nameLength);
console.log(`First Character: ` + firstChar);
    

Booleans

Booleans represent true or false values and are often used in conditional statements. Here's how to work with booleans:

// Declare booleans
let isStudent = true;
let isWorking = false;
// Use in conditional statements
if (isStudent) {
    console.log(`You are a student.`);
} else {
    console.log(`You are not a student.`);
}
if (isWorking) {
    console.log(`You are working.`);
} else {
    console.log(`You are not working.`);
}
    

Conclusion

Understanding JavaScript data types is fundamental for any programming task. Numbers, strings, and booleans are just a few of the data types you'll encounter when working with JavaScript. They serve as the building blocks for more complex data manipulation and logic in your code.

Happy coding!

Written by Surfside Media

Senior Full Stack Developer specializing in Web Technologies.