0

I thought this MDN page was an exhaustive list of literals. I was then told function expressions were also literals (something the MDN page didn't mention).

My current understanding of all the literals in JavaScript is:

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}).

Same to a previous question I asked, I'm wondering why:

  1. The primitive value undefined is not a literal.

  2. The purpose of the term 'literal'. Why have we created this term?

i) This article says: a literal is

something that has a fixed value that was set by a programmer during the creation of the code.

ii) And this article contrasts literals with variables saying variables are often initialised by literals.

I'm just left wondering why the primitive undefined is not a literal since i) it seems like a fixed value and ii) it can initialise a variable

  • 1
    ["In computer science, a literal is a notation for representing a fixed value in source code."](https://en.wikipedia.org/wiki/Literal_\(computer_programming\)). Note, that due to legacy reasons, `undefined` is not a keyword, but a property of the global object. Strictly speaking, when writing `let v = undefined;`, you could be assigning something else than the value `undefined`. However, i see that as very nit-picky, and would consider `undefined` as a literal, as e.g. shadowing `undefined` is a really absurd case. Strictly speaking, that problem exists though. – ASDFGerte Aug 01 '20 at 13:25
  • However, this has already been mentioned in your linked previous question, so i don't really understand, what aspect you are still confused about. – ASDFGerte Aug 01 '20 at 13:27
  • @ASDFGerte I don't understand what it being a global property has to do with anything. The property undefined returns the primitive value, `undefined` and it's this primitive value I'm concerned about – tonitone117 Aug 01 '20 at 13:28
  • 1
    `"use strict"; function f(undefined) { console.log(undefined); }; f("totally undefined");` – ASDFGerte Aug 01 '20 at 13:29
  • What's that meant to be showing me? – tonitone117 Aug 01 '20 at 13:29
  • That `undefined` is not a primitive value, or even any predefined value, in that case. It's a legacy problem of javascript. – ASDFGerte Aug 01 '20 at 13:30
  • See also https://stackoverflow.com/questions/11167317/what-is-the-purpose-of-a-parameter-named-undefined-in-a-javascript-function and related questions about how `undefined` works (and worked before) – Bergi Aug 01 '20 at 13:55
  • A literal must either take a syntactic form other than an identifier, or it must be a keyword (reserved word that's not allowed for usage as an identifier). `undefined` is not a reserved word for mostly historical accident, and that causes it to not be counted as a literal. Using `undefined` in your code is a variable reference. – Bergi Aug 01 '20 at 13:57
  • As a side note, i wonder, whether one could start a flamewar, about whether `void 0` is a literal or not. I mean, strictly speaking, it's an operator first, but will any engine actually treat it differently than just immediately inserting the value `undefined`? – ASDFGerte Aug 01 '20 at 13:59
  • 1
    @ASDFGerte It's clearly a unary expression with a literal as the operand. Yes, it will probably be constant-folded, but that definitely doesn't make it a literal. Same for e.g. `5+1`. – Bergi Aug 01 '20 at 14:54

0 Answers0