Page 199 - Web_Application_v2.0_C12_Fb
P. 199
typeof Operator
typeof is a type checking operator in JavaScript that returns the datatype of the operand passed to it. The
argument for the type of operator can be any variable, function, or object whose type you want to determine.
Example 10: To demonstrate the use of typeof operator
<!DOCTYPE HTML>
<HTML>
<HEAD>
<SCRIPT>
document.write(typeof 'UMBRELLA'+"<BR>")
document.write(typeof 786 +"<BR>")
document.write(typeof false +"<BR>")
document.write(typeof true)
</SCRIPT>
</HEAD>
</HTML>
Output:
Following is a list of return values for the typeof operator:
Type String Return Type
Number “number”
String “string”
Boolean “boolean”
Object “object”
Function “function”
Undefined “undefined”
Null “object”
Notes
In JavaScript, both console.log() and document.write() are used to display output, but they serve different
purposes and are used in different contexts. The console.log() method logs messages to the browser’s
console and is mainly used for debugging and checking values while writing and testing code. On the
other hand, the document.write() method writes HTML content directly to the web page and is typically
used for displaying simple messages. However, it is not recommended for modern web development as
it can overwrite the entire page if used after the document has loaded.
2.6 FUNCTIONS IN JAVASCRIPT
A block of code created specifically to carry out a specific task is referred to as a JavaScript function. If
something calls a JavaScript function, it will be executed (a function call).
The function keyword, a name, and parentheses define a JavaScript function.
JavaScript Part 2 197

