What is Undefined X1 in JavaScript


In order to look at the outcome of Undefined X 1 in JavaScript, we first need to understand exactly Undefined in JavaScript represents. JavaScript has a keyword called undefined that has something to do with memory. The global object has the attribute undefined. In other words, it is a variable with a global scope. The primitive value of undefined is its starting value.

Undefined is the type of a variable which has not been given a value. If a variable is being evaluated but doesn't have an assigned value, a method or statement will also return undefined. If a value had not been returned, a function throws undefined.

Until the memory space is established, everything in Javascript that receives a memory space is given Undefined. Therefore, in JavaScript, the result of Undefined X 1 is NaN (Not a Number).

The JavaScript abbreviation NaN stands for Not a Number and indicates a non-writable property, or a value that is not a number. Although NaN is hardly used in programmes, it allows us to verify whether the number we have specified is correct or not.

Syntax

undefined

Example 1

In order to see the result of Undefined X 1, we will first begin ‘y’ with undefined and thereafter multiply it by 1. The example below demonstrates an indeterminate form operation; we are multiplying 1 by undefined, which is a keyword and an invalid number because it has not even been created. The outcome of multiplying it by a number now is NaN.

<!DOCTYPE html> <html> <title>What is Undefined X 1 in JavaScript - TutorialsPoint</title> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body style="text-align:center"> <script> let y = undefined; document.write(y * 1); </script> </body> </html>

Example 2

In this example let us understand when we trying to convert a string to an integer, NaN will be returned.

<!DOCTYPE html> <html> <title>What is Undefined X 1 in JavaScript - TutorialsPoint</title> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body style="text-align:center"> <script> document.write(parseInt("Tutorialspoint has free Online Tutorials and Courses.")); </script> </body> </html>

Example 3

In this example let us understand how a variable's value would be undefined if it is declared however its value is not yet assigned. Additionally, a variable may be explicitly given the value undefined.

<!DOCTYPE html> <html> <title>What is Undefined X 1 in JavaScript - TutorialsPoint</title> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body style="text-align:center"> <script> let student = "Bob Smith"; // placing an unknown value into the student variable student = undefined document.write(student); // returns undefined </script> </body> </html>

Example 4

In this example let us understand how the undefined and null are considered false values in JavaScript.

<!DOCTYPE html> <html> <title>What is Undefined X 1 in JavaScript - TutorialsPoint</title> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body style="text-align:center"> <script> if(null || undefined ) { document.write('value of null is true'); } else { document.write('value of null is false'); } </script> </body> </html>

Example 5

In this example let us understand, If the Boolean() function is called with an undefined or null, it will result in false.

<!DOCTYPE html> <html> <title>What is Undefined X 1 in JavaScript - TutorialsPoint</title> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body style="text-align:center"> <script> let myResult; myResult = Boolean(undefined); document.write(myResult +'<br>'); // returns false myResult = Boolean(null); document.write(myResult); // returns false </script> </body> </html>

Example 6

JavaScript treats null as an object. The typeof operator has the ability to verify this. The variables and values' types are decided by the typeof operator. The undefined value is returned by the typeof operator when it is used to find the undefined value.

<!DOCTYPE html> <html> <title>What is Undefined X 1 in JavaScript - TutorialsPoint</title> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body style="text-align:center"> <script> const x = null; document.write(typeof x +'<br>'); // object let y; document.write(typeof y); // undefined </script> </body> </html>

Example 7

In this example let us understand the JavaScript Default Values null and undefined When undefined is passed to a function argument in JavaScript which accepts a default value, the default value gets used in place of the undefined.

<!DOCTYPE html> <html> <title>What is Undefined X 1 in JavaScript - TutorialsPoint</title> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body style="text-align:center"> <script> function tutpoint(b = 1) { document.write(b); } // pass undefined // it takes default value as 1 tutpoint(undefined); </script> </body> </html>

Example 8

On the other hand, a default parameter function accepts null as a value when it is passed null.

<!DOCTYPE html> <html> <title>What is Undefined X 1 in JavaScript - TutorialsPoint</title> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body style="text-align:center"> <script> function tutpoint(b = 1) { document.write(b); } // pass undefined // takes null tutpoint(null); </script> </body> </html>

Updated on: 24-Aug-2022

237 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements