What are the different data types in JavaScript?

JavaScript has several data types, including:

Number: represents both integer and floating-point numbers.
String: represents a sequence of characters enclosed in quotes.
Boolean: represents a logical entity and can have only two values, either true or false.
Null: represents a deliberate non-value or absence of any object value.
Undefined: represents a declared variable that has not been assigned a value.
Object: represents a collection of key-value pairs or properties and methods.
Symbol: represents a unique identifier introduced in ECMAScript 2015.

Numbers in JavaScript

In JavaScript, you can declare numbers using the "number" data type. Here are some examples:

// Declaration of numbers
let num1 = 10; // An integer number
let num2 = 3.14; // A floating-point number

// Mathematical operations with numbers
let sum = num1 + num2; // Addition
let diff = num1 - num2; // Subtraction
let product = num1 * num2; // Multiplication
let quotient = num1 / num2; // Division

console.log(sum); // Output: 13.14
console.log(diff); // Output: 6.86
console.log(product); // Output: 31.4
console.log(quotient); // Output: 3.1847133757961785

In the example above, we declare two numbers, "num1" and "num2", and then perform various mathematical operations with them using operators such as "+" (addition), "-" (subtraction), "*" (multiplication), and "/" (division). We then log the results of these operations to the console using the "console.log()" method.

Strings in JavaScript

In JavaScript, you can declare strings using the "string" data type. Here are some examples:

// Declaration of strings
let str1 = "Hello, world!"; // using double quotes
let str2 = 'JavaScript is awesome'; // using single quotes
let str3 = `The result is ${num1 + num2}`; // using backticks for template literals

// String concatenation
let fullName = "Deepak" + " " + "Tewatia"; // concatenating two strings
let greeting = "Hello, " + fullName; // concatenating string with variable

// String methods
let sentence = "The quick brown fox jumps over the lazy dog.";
let length = sentence.length; // get the length of a string
let upperCase = sentence.toUpperCase(); // convert string to uppercase
let lowerCase = sentence.toLowerCase(); // convert string to lowercase
let subString = sentence.substring(4, 19); // get a substring
let split = sentence.split(" "); // split string into an array of substrings

console.log(str1); // Output: Hello, world!
console.log(str2); // Output: JavaScript is awesome
console.log(str3); // Output: The result is 13.14

In the examples above, we declare three strings using different ways of quotes. We then perform string concatenation using the "+" operator and demonstrate some commonly used string methods such as "length", "toUpperCase()", "toLowerCase()", "substring()", and "split()". These methods allow us to manipulate and work with string data in various ways.

Boolean in JavaScript

In JavaScript, you can declare boolean values using the "boolean" data type, which can have only two possible values: true or false. Here are some examples:

// Declaration of boolean values
let isTrue = true;
let isFalse = false;

// Boolean operations
let result1 = isTrue && isFalse; // logical AND
let result2 = isTrue || isFalse; // logical OR
let result3 = !isTrue; // logical NOT

console.log(result1); // Output: false
console.log(result2); // Output: true
console.log(result3); // Output: false

In the example above, we declare two boolean values, "isTrue" and "isFalse", and then perform some logical operations using operators such as "&&" (logical AND), "||" (logical OR), and "!" (logical NOT). We then log the results of these operations to the console using the "console.log()" method.

Boolean values are often used in conditional statements, such as "if" statements, to control the flow of program execution based on whether a condition is true or false. For example:

let age = 18;

if (age >= 18) {
    console.log("You are an adult.");
} else {
    console.log("You are a minor.");

In this example, we use the "if" statement to check if the "age" variable is greater than or equal to 18. If it is, we log the message "You are an adult." to the console. Otherwise, we log the message "You are a minor." to the console.