JavaScript is a versatile programming language that supports various data types and operators. In this explanation, we'll delve into primitive data types and operators in JavaScript, exploring their characteristics and usage with examples.


Primitive Data Types in JavaScript
JavaScript has six primitive data types:
 
  • Number: Represents numeric values. It includes 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 an intentional absence of any value.
  • Symbol: Represents a unique identifier introduced in ECMAScript 6.

Operators in JavaScript

JavaScript includes various operators to perform operations on data. Here are some common operators:

Arithmetic Operators: Used for arithmetic calculations like addition (+), subtraction (-), multiplication (*), division (/), and modulus (%).

Comparison Operators: Used to compare values and return a boolean result. Examples include equal to (==), not equal to (!=), strict equal to (===), greater than (>), less than (<), etc.

Logical Operators: Used for logical operations. Includes AND (&&), OR (||), and NOT (!).

Assignment Operators: Used to assign values to variables. Examples are the assignment operator (=), addition assignment (+=), subtraction assignment (-=), etc.

Unary Operators: Operators that work on a single operand. Examples are the increment (++) and decrement (--) operators.

Ternary Operator (Conditional Operator): Used for conditional expressions. It has the syntax condition ? value1 : value2.

Bitwise Operators: Used to perform bitwise operations on integers. Examples include bitwise AND (&), bitwise OR (|), bitwise XOR (^), etc.


Examples and Explanation

Number Data Type and Arithmetic Operators
 


let num1 = 10;
let num2 = 5;
let sum = num1 + num2; // Addition
let difference = num1 - num2; // Subtraction
let product = num1 * num2; // Multiplication
let quotient = num1 / num2; // Division
let remainder = num1 % num2; // Modulus
 
 

In this example, we use the number data type and arithmetic operators to perform basic mathematical operations.


String Data Type and Concatenation Operator

 
let firstName = "John";
let lastName = "Doe";
let fullName = firstName + " " + lastName; // Concatenation
 

Here, we use the string data type and the concatenation operator (+) to combine two strings.


Boolean Data Type and Comparison Operators

 
let age = 20;
let isAdult = age >= 18; // Greater than or equal to comparison
let isValid = true;
let isInvalid = !isValid; // Logical NOT operator
 

In this snippet, we use the boolean data type and comparison/logical operators to evaluate conditions and determine boolean values.


Undefined and Null Data Types

 
let undefinedVar; // Undefined variable
let nullVar = null; // Null variable
 

Here, we declare an undefined variable and explicitly assign null to another variable.


Symbol Data Type

 
const key = Symbol("key");
let obj = {
  [key]: "value"
};

In this example, we use symbols as unique property keys in an object.


Ternary Operator

 
let num = 15;
let result = num % 2 === 0 ? "Even" : "Odd";

This code snippet uses the ternary operator to check if a number is even or odd and assigns the result accordingly.


Bitwise Operators
 
let a = 5; // 0101
let b = 3; // 0011
let bitwiseAnd = a & b; // Bitwise AND (0001)
let bitwiseOr = a | b; // Bitwise OR (0111)
let bitwiseXor = a ^ b; // Bitwise XOR (0110)

These examples demonstrate bitwise AND, OR, and XOR operations on binary numbers.
 



Practice Excercise Practice now