Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What JavaScript value should you assign to a variable x so that typeof x = = = x would be true?

I've tried assigning different data types such as boolean, numbers, strings, objects, nulls, etc, but keep getting false returned. Any ideas?

like image 579
Almac Avatar asked Jun 28 '15 04:06

Almac


People also ask

How do you assign a variable in JavaScript?

To create a variable in JavaScript, use the let keyword. To be concise, we can combine the variable declaration and assignment into a single line: let message = 'Hello! '; // define the variable and assign the value alert(message); // Hello!

How do you check what the type of a value in variable x is in JavaScript?

typeof is a JavaScript keyword that will return the type of a variable when you call it. You can use this to validate function parameters or check if variables are defined. There are other uses as well. The typeof operator is useful because it is an easy way to check the type of a variable in your code.

What is the typeof operator in JavaScript?

Typeof in JavaScript is an operator used for type checking and returns the data type of the operand passed to it. The operand can be any variable, function, or object whose type you want to find out using the typeof operator.

What does => mean in JavaScript?

It's a new feature that introduced in ES6 and is called arrow function. The left part denotes the input of a function and the right part the output of that function.


1 Answers

The typeof operator always returns a string value. As such, your original value must be a string. The result of typeof for a string is "string", and so:

x = "string"
typeof x === x // true
like image 125
Phrogz Avatar answered Sep 21 '22 10:09

Phrogz