JavaScript Miscellaneous Operators

Special operators for advanced JavaScript operations

🛠️ What are Miscellaneous Operators?

JavaScript has several special operators that don't fit into the main categories. These operators provide powerful functionality for type checking, conditional operations, and more.


// Ternary operator example
let age = 18;
let status = age >= 18 ? "adult" : "minor";
console.log(status); // "adult"
                                    

Output:

adult

JavaScript Miscellaneous Operators

Ternary (?:)

Conditional operator for if-else

condition ? value1 : value2
🔍

typeof

Returns the type of a variable

typeof "hello" // "string"
🏗️

instanceof

Checks if object is instance of class

[] instanceof Array // true
🗑️

delete

Removes property from object

delete obj.property

🔹 Ternary Operator Examples

The ternary operator is a shorthand for if-else statements:

// Basic ternary
let score = 85;
let grade = score >= 90 ? "A" : score >= 80 ? "B" : "C";
console.log(grade); // "B"

// With functions
function getDiscount(isMember) {
    return isMember ? 0.1 : 0;
}

console.log(getDiscount(true));  // 0.1
console.log(getDiscount(false)); // 0

// Nested ternary (use sparingly)
let weather = "sunny";
let activity = weather === "sunny" ? "beach" : 
               weather === "rainy" ? "movie" : "walk";
console.log(activity); // "beach"

Output:

B
0.1
0
beach

🔹 typeof Operator

Use typeof to check the data type of variables:

// Different data types
console.log(typeof 42);          // "number"
console.log(typeof "hello");     // "string"
console.log(typeof true);        // "boolean"
console.log(typeof undefined);   // "undefined"
console.log(typeof null);        // "object" (this is a known quirk)
console.log(typeof {});          // "object"
console.log(typeof []);          // "object"
console.log(typeof function(){}); // "function"

// Practical use
function processValue(value) {
    if (typeof value === "string") {
        return value.toUpperCase();
    } else if (typeof value === "number") {
        return value * 2;
    }
    return value;
}

console.log(processValue("hello")); // "HELLO"
console.log(processValue(5));       // 10

Output:

number
string
boolean
undefined
object
object
object
function
HELLO
10

🔹 instanceof and delete Operators

instanceof checks object types, delete removes properties:

// instanceof examples
let arr = [1, 2, 3];
let date = new Date();
let obj = {};

console.log(arr instanceof Array);  // true
console.log(date instanceof Date);  // true
console.log(obj instanceof Object); // true
console.log(arr instanceof Object); // true (Array extends Object)

// delete operator
let person = {
    name: "John",
    age: 30,
    city: "New York"
};

console.log(person); // {name: "John", age: 30, city: "New York"}

delete person.age;
console.log(person); // {name: "John", city: "New York"}

// delete returns true if successful
console.log(delete person.city); // true
console.log(person); // {name: "John"}

Output:

true
true
true
true
{name: "John", age: 30, city: "New York"}
{name: "John", city: "New York"}
true
{name: "John"}

🧠 Test Your Knowledge

What does typeof null return?