0

This page lists literals. I presume it's exhaustive (displays all possible literals).

It lists: string literals ("str"|'str'); template literals; numeric literals i.e. 5; boolean literals i.e. true; null literal; array literals ([1, 2]) and object literals ({num: 5}).

Why are undefined and function declaration/expressions not literals?

Edit: I now know that literals are in contrast to variables and can sometimes be used to assign variables which is why function expressions and not function declarations are considered literals.

  • null is not boolean literal – Umutambyi Gad Jul 29 '20 at 13:26
  • 1
    `undefined` is a variable in global scope referring to a primitive value, *not* a literal: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined – jonrsharpe Jul 29 '20 at 13:27
  • @jonrsharpe is the primitive value it refers to not `undefined`? – tonitone117 Jul 29 '20 at 13:31
  • Yes: https://developer.mozilla.org/en-US/docs/Glossary/undefined – jonrsharpe Jul 29 '20 at 13:31
  • @jonrshape ok, so my question is what is the purpose of the term literal? Why wouldn't undefined and function declarations/expressions be considered literals? – tonitone117 Jul 29 '20 at 13:33
  • At first we need to define ["_literal_"](https://en.wikipedia.org/wiki/Literal_(computer_programming)). According to the linked Wiki article, anonymous function expressions are also literals. – Teemu Jul 29 '20 at 14:03
  • @Teemu that's good to know! I wonder why it wasn't mentioned in the spec I hyperlinked. I also wonder if the `undefined` primitive value is a literal as well. – tonitone117 Jul 29 '20 at 15:23
  • 1
    @tonitone117 It isn't. `undefined` is a property in the global scope that holds the primitive value `undefined`. It's as much of a literal as `alert` is. In the old days you could even assign a different value to `undefined`. You can't do that to literals. – Ivar Jul 29 '20 at 15:56
  • @Ivar I just meant if the `undefined` *primitive* was a literal – tonitone117 Jul 29 '20 at 22:27
  • @tonitone117 There is the `undefined` global property and the `undefined` primitive value. The primitive value has no literal. It is automatically assigned to variables that don't have any value explicitly set, as well arguments that aren't passed etc. The value is as well assigned to the global property `undefined`, but that does not make this a literal. Like I mentioned, the `undefined` that you use in your code is no different from the global property `alert` that you can use. Neither of them are literals.This is unlike things like `true`. – Ivar Jul 30 '20 at 06:52
  • If you use `undefined = '';` in strict mode you get an error "_Uncaught TypeError: Cannot assign to read only property 'undefined' of object '#'_". If you declare it as a new variable in the global scope you'll get "_Identifier 'undefined' has already been declared_". If you declare it in a function, you will actually create a new variable named `undefined` which you can use within that scope. If you assign something to `true` it will always result in a syntax error. (Either "_Invalid left-hand side in assignment_" or "_Unexpected token 'true'_".) – Ivar Jul 30 '20 at 07:07
  • @Ivar Thanks for that what I got from it is every-time the coder types `undefined` they are referencing the global property `undefined` which returns the primitive value `undefined`. And things like uninitialised variables and parameters with no argument return the primitive `undefined`. In any case, I'm still left wondering why the primitive value is not considered a literal? – tonitone117 Jul 30 '20 at 12:43
  • And also, what is the purpose of the word 'literal'. Wikipedia told me literals are often used in contrast to variables. – tonitone117 Jul 30 '20 at 13:33

0 Answers0