Course Progress 100%

JavaScript Type Operators

JavaScript में Type Operators का उपयोग किसी value या variable के datatype को check करने के लिए किया जाता है। जब हम programming करते हैं, तो यह जानना बहुत जरूरी होता है कि किसी variable में किस type का data store है, जैसे number, string, object या function।

Beginner के लिए Type Operators समझना इसलिए जरूरी है क्योंकि JavaScript एक dynamically typed language है, यानी variable का datatype runtime पर decide होता है।

typeof Operator

typeof operator JavaScript का सबसे commonly used type operator है। इसका उपयोग यह पता लगाने के लिए किया जाता है कि किसी variable या value का datatype क्या है।

Example: typeof Operator with Number

<script>
let age = 25;
let result = typeof age;
</script>

यह code age variable का datatype check करता है।

क्योंकि 25 एक number है, इसलिए typeof age का result "number" होगा।

Final output memory में result = "number" store होगा।

typeof operator इसलिए use किया जाता है ताकि program को पता चल सके कि variable में किस type का data है।

typeof Operator with String

Example: typeof with String

<script>
let name = "Rahul";
let output = typeof name;
</script>

यह code name variable का datatype check करता है।

क्योंकि "Rahul" text है, इसलिए result "string" होगा।

Final output memory में output = "string" store होगा।

typeof Operator with Boolean

Example: typeof with Boolean

<script>
let isPassed = true;
let check = typeof isPassed;
</script>

यह code Boolean value का datatype check करता है।

Result "boolean" होगा।

Final output memory में check = "boolean" store होगा।

typeof Operator with Undefined

Example: typeof with Undefined

<script>
let value;
let result = typeof value;
</script>

यहां variable declare किया गया है लेकिन उसमें कोई value assign नहीं की गई।

ऐसे case में datatype undefined होता है।

Final output memory में result = "undefined" store होगा।

typeof Operator with Object

Example: typeof with Object

<script>
let student = { name: "Amit", age: 20 };
let result = typeof student;
</script>

यह code object का datatype check करता है।

Result "object" होगा।

Final output memory में result = "object" store होगा।

Objects JavaScript में complex data store करने के लिए use होते हैं।

typeof Operator with Array

Array technically object ही होता है, इसलिए typeof array के लिए भी "object" return करता है।

Example: typeof with Array

<script>
let numbers = [10, 20, 30];
let result = typeof numbers;
</script>

यह code array का datatype check करता है।

Result "object" होगा।

Final output memory में result = "object" store होगा।

इसलिए array check करने के लिए typeof हमेशा correct नहीं माना जाता।

typeof Operator with Function

Example: typeof with Function

<script>
function greet() {
  return "Hello";
}

let result = typeof greet;
</script>

यह code function का datatype check करता है।

Result "function" होगा।

Final output memory में result = "function" store होगा।

JavaScript में function भी एक special type का object माना जाता है।

typeof Operator with Null

यह JavaScript का एक confusing behavior है।

Example: typeof with null

<script>
let data = null;
let result = typeof data;
</script>

यह code null का datatype check करता है।

Unexpected रूप से result "object" आता है।

Final output memory में result = "object" store होगा।

यह JavaScript की old bug है, जिसे backward compatibility की वजह से fix नहीं किया गया।

instanceof Operator

instanceof operator check करता है कि कोई object किसी specific constructor या class से बना है या नहीं।

Example: instanceof Operator

<script>
let fruits = ["apple", "banana"];
let result = fruits instanceof Array;
</script>

यह code check करता है कि fruits variable Array से बना है या नहीं।

क्योंकि fruits एक array है, result true होगा।

Final output memory में result = true store होगा।

instanceof operator इसलिए use किया जाता है ताकि object की actual type accurately check की जा सके।

instanceof with Object

Example: instanceof with Object

<script>
let person = { name: "Ravi" };
let check = person instanceof Object;
</script>

यह code check करता है कि person Object से बना है या नहीं।

Result true होगा।

Final output memory में check = true store होगा।

Practical Use of Type Operators

Type operators का use:

  • Input validation में
  • Error handling में
  • Dynamic logic बनाने में
  • Safe और reliable JavaScript code लिखने में

Type operators JavaScript में datatype awareness लाते हैं, जिससे program ज्यादा stable और beginner-friendly बनता है।