What does "literal" mean?? string literal? object literal? literal syntax???
Me too have wondered what is the meaning of literal that is shoved between text while the text itself makes sense. String literal they say, and I am wondering why not just say "string". Literal syntax of declaring objects !!
Okay, Okay, enough talk, more action (or technically... more talk).
Literal is a notation (way of writing) for representing values. It is the "literal" notation of representing values. And literal here means the same meaning we know it for i.e. literally. Lets make it more clear.
let a = "boboddy"
a is a string. "boboddy" is also a string.
What is the difference between the two.
One is a variable representing the string, the other is the "literal" way of representing the string.
SO variables and constants cant be string literals or any value literal.
let stringVar = String("something")
so String("something")
here is a value right? and it is a string because the string method will return a string as an output (it will get stored to stringVar but we are not talking about the variable here, we are talking about String("something")
as a value. It is a string but it is not a string literal
because it is not a literal string notation, the literal string notation is "something")
and the same goes for the other kind of values.
5
is an integer literal but 2 + 3
is not. Neither is Number("5")
. They are integers (in the sense that their 2 +3
expression will evaluate to 5
and the same goes for the other but they are not integer literals)
You would often hear about the literal syntax of declaring an object. It is like that:
let car = {
model: "BMW",
year: 2012
}
because those curly braces and the way of writing the key value pairs is the literal syntax.
let car = new Object();
car.model = "BMW"
car.year = 2012
That is another way of declaring an object but it is not the literal way.
I hope this is clear now.
Thank you for taking the time to read this article. If you found it helpful, leave a like or a comment. If you have any questions, feel free to ask them down below.