TypeOf Operator in JavaScript
The “typeof” operator in JavaScript is a unary operator that checks data types of its operands. The operand can be either literal, variable, function, or object.
The typeof operator has only one operand. It returns the type of variable of the given operand. It is very useful for debugging.
With the help of typeof operator, we can determine whether a declared variable in a program is being as a string, a number, or a boolean; or that variable is a certain type object or function.
The general syntax of typeof operator in JavaScript is as follows:
typeof(operand);
or,
typeof operand;
Return Values for typeof Operator
The below table lists the possible return values of typeof operator.
| Operand Type | Return Value |
|---|---|
| String literal | “string” |
| Number literal | “number” |
| Boolean literal (true or false) | “boolean” |
| Undefined literal | “undefined” |
| Null literal | “null” |
| Symbol | “symbol” |
| Function | “function” |
| Anything else | “object” |
Let’s take an example program, in which we will check the data type of variable with the help of typeof operator.
Example 1:
<html>
<head>
<title>JavaScript typeof Operator Example</title>
</head>
<body>
<script>
let type;
let num = 20;
type = typeof(num);
document.write("Type of variable num: " +type, "<br>");
let str = "Programming";
type = typeof(str);
document.write("Type of variable str: " +type, "<br>");
let boo = true;
type = typeof(boo);
document.write("Type of variable boo: " +type, "<br>");
let value = null;
type = typeof(value);
document.write("Type of variable value: " +type, "<br>");
</script>
</body>
</html>
Output:
Type of variable num: number
Type of variable str: string
Type of variable boo: boolean
Type of variable value: object
Delete Operator in JavaScript
The delete operator in JavaScript is a unary operator that deletes a property of an object, or array element. It is a way to remove the property name from the list of keys in the object or array. The delete operator has only one operand.
The delete unary operator returns true if the property of an object or array element has deleted. Let’s take an example program in which we will delete properties of an object and array elements with help of delete operator.
Example 2:
<html>
<head>
<title>JavaScript Delete Operator Example</title>
</head>
<body>
<script>
// Declaring an object.
let firstName = {name1 : "Johm", name2 : "Deep", name3 : "Ivaan", name4 : "Amit"};
document.write(delete firstName.name1, "<br>"); // return true.
document.write(delete firstName.name2, "<br>"); // returns true.
document.write(delete firstName, "<br>"); // return false.
// Declaring an array.
var primeNums = [2, 3, 5, 7, 11];
document.write(delete primeNums[11], "<br>"); // returns true.
document.write(delete primeNums); // returns false.
</script>
</body>
</html>
Output:
true
true
false
true
false
The delete operator deletes the properties of objects or arrays. It does not delete declared variables or functions.
Note: The delete operator is not used to free up memory
Void Operator in JavaScript
The void operator in JavaScript is a unary operator that evaluates an expression without returning a value. It has a single operand, which may be of any type.
The void operator first evaluates its operand, then discard the value and returns undefined. It is mainly used in an HTML <a> tag, which will prevent the browser from navigating into a new page:
<a href = "javascript:void 0 ">Do nothing</a>
Let’s take a very simple example program based on JavaScript void operator. Take a look at the below script code.
Example 3:
<html>
<head>
<title>JavaScript Void Operator Example</title>
</head>
<body>
<script>
// Declaring a function.
function m1()
{
document.write("I am in m1 function", "<br>");
return true;
}
document.write(m1(), "<br>");
function m2()
{
document.write("I am in m2 function", "<br>");
return true;
}
document.write(void m2());
</script>
</body>
</html>
Output:
I am in m1 function
true
I am in m2 function
undefined
As you can observe in the script code, the function m1() displays two statements: I am in m1 function and true.
The second function m2() also generate two statements, but the second one reads undefined because the void operator evaluates the function without returning a value.
In this tutorial, you learned typeof operator in JavaScript with example program. I hope that you will have understood the basic concepts of delete and void operators in JavaScript.
Thanks for reading!!!




