No announcement
 

Data Types and Operators

Introduction to Data Types:

In JavaScript, data types represent the kind of values that can be stored and manipulated. JavaScript is a dynamically-typed language, which means variables can hold different types of data at different times during the program's execution. JavaScript has several built-in data types, including primitive types and reference types.

Primitive Data Types:

  • Number: Represents numeric values, including integers and floating-point numbers.
  • String: Represents textual data, enclosed in single ('') or double ("") quotes.
  • Boolean: Represents a logical value, either true or false.
  • Undefined: Represents a variable that has been declared but not assigned a value.
  • Null: Represents the intentional absence of a value.
  • Symbol: Represents a unique and immutable value, introduced in ES6.

Reference Data Types:

  • Object: Represents complex data structures, including key-value pairs and arrays.
  • Array: Represents an ordered list of elements, enclosed in square brackets [].

Example:

datatype.html

<!DOCTYPE html>
<html>
<head>
  <title>JavaScript Data Types Example</title>
</head>
<body>

<h2>JavaScript Data Types:</h2>
<script>
let numberValue = 42;
let stringValue = "Hello, World!";
let booleanValue = true;
let undefinedValue;
let nullValue = null;
let symbolValue = Symbol("unique");

let person = {
  name: "John",
  age: 30,
};

let fruits = ["apple", "banana", "orange"];

console.log(typeof numberValue); // Output: number
console.log(typeof stringValue); // Output: string
console.log(typeof booleanValue); // Output: boolean
console.log(typeof undefinedValue); // Output: undefined
console.log(typeof nullValue); // Output: object
console.log(typeof symbolValue); // Output: symbol

console.log(typeof person); // Output: object
console.log(typeof fruits); // Output: object
</script>

</body>
</html>

Output
number string boolean undefined object symbol object object

In the above example, we have demonstrated various JavaScript data types, including primitive and reference types. We used the typeof operator to check the type of each variable.

JavaScript Operators:

Operators are symbols or keywords that perform operations on operands (values). JavaScript has various types of operators:

  • Arithmetic Operators: Perform basic arithmetic operations like addition, subtraction, multiplication, division, and more.
  • Comparison Operators: Compare values and return a Boolean value (true or false) based on the comparison.
  • Logical Operators: Perform logical operations like AND (&&), OR (||), and NOT (!).
  • Assignment Operators: Assign values to variables.
  • Conditional (Ternary) Operator: A shorthand way to write conditional statements.

Example:

operators.html

<!DOCTYPE html>
<html>
<head>
  <title>JavaScript Operators Example</title>
</head>
<body>

<h2>JavaScript Operators:</h2>
<script>
let a = 5;
let b = 10;
let result;

// Arithmetic Operators
result = a + b;
console.log("Addition:", result); // Output: Addition: 15

result = a - b;
console.log("Subtraction:", result); // Output: Subtraction: -5

result = a * b;
console.log("Multiplication:", result); // Output: Multiplication: 50

result = b / a;
console.log("Division:", result); // Output: Division: 2

result = b % a;
console.log("Modulus:", result); // Output: Modulus: 0

// Comparison Operators
result = a > b;
console.log("Greater than:", result); // Output: Greater than: false

result = a < b;
console.log("Less than:", result); // Output: Less than: true

result = a >= b;
console.log("Greater than or equal:", result); // Output: Greater than or equal: false

result = a <= b;
console.log("Less than or equal:", result); // Output: Less than or equal: true

result = a === b;
console.log("Equal:", result); // Output: Equal: false

result = a !== b;
console.log("Not Equal:", result); // Output: Not Equal: true

// Logical Operators
let x = true;
let y = false;
result = x && y;
console.log("Logical AND:", result); // Output: Logical AND: false

result = x || y;
console.log("Logical OR:", result); // Output: Logical OR: true

result = !x;
console.log("Logical NOT:", result); // Output: Logical NOT: false

// Assignment Operators
let c = 15;
c += 5; // c = c + 5
console.log("Addition Assignment:", c); // Output: Addition Assignment: 20

// Conditional (Ternary) Operator
let age = 18;
let isAdult = age >= 18 ? "Adult" : "Minor";
console.log("Is Adult?", isAdult); // Output: Is Adult? Adult
</script>

</body>
</html>

Output
Addition: 15 Subtraction: -5 Multiplication: 50 Division: 2 Modulus: 0 Greater than: false Less than: true Greater than or equal: false Less than or equal: true Equal: false Not Equal: true Logical AND: false Logical OR: true Logical NOT: false Addition Assignment: 20 Is Adult? Adult