What literals return true?

(typeof %литерал% == "undefined") 

Closed due to the fact that the essence of the question is not clear to the participants of Vadim Ovchinnikov , Alex , user194374, Grundy , Kromster 9 Jan '17 at 8:24 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • I think that everything defined by the language specification, literals, will return false. Is not it so? - Vlad from Moscow
  • the point is that there is a universal function that has such an argument, which may or may not be known, i.e. checking for the existence of an argument looks like typeof arg == "undefined". If I want to leave it 'unspecified', but there are more arguments following it, I want to put some kind of stub - Meow
  • skip this argument completely when calling a function, and inside check arguments.length - Igor
  • this is very cumbersome, the function is very large, and the number of arguments varies greatly. previously checked arg == "", but after recent edits something went wrong. It seems to have found a solution in the form of void () - Meow
  • @Igor, no, the option with undefined is more correct, since this is how the default values ​​work in ES6. - Qwertiy

2 answers 2

Found two solutions to the problem

Trite but

 typeof undefined == "undefined" 

although no one bothers to set this variable to undefined and it turns out insecure

As a result, the solution is void (0), since an attempt to declare a void function will cause an error

 typeof void(0) == "undefined" 

Although it still turns out some zashkvar :)

  • one
    Global undefined has long been impossible to set, and for their variables and you can follow himself. A void is not a function, but a unary operator. - Qwertiy

It depends on what is meant by a literal. If constant, then no.

If anything, undefined (this is a readonly property, not a constant), any undeclared variables (except for the variables in the temporal dead zone in ES6), any expressions whose values ​​are undefined .

Well, another magical event - document.all :)

 console.log(typeof undefined == 'undefined') console.log(typeof gdfsfjgsb == 'undefined') console.log(typeof void 6789 == 'undefined') console.log(typeof {}.jsdhfs == 'undefined') console.log(typeof document.all == 'undefined') console.log(document.all.length) 

  • funny :-) why is HTMLAllCollection undefined? - Grundy
  • @Grundy, for historical reasons. This was a non-standard feature of IE6, which was actively used to verify that the site was opened in IE6. And others wanted to have such sites working for them, and others did not send with the message that IE is not supported. By the way, I don’t know if something is written about this in the specification) - Qwertiy