String value vs string literal

What is the difference between string value and string literal?

Were getting into distinctions here, but the way I think of it is that a string value is the value stored into a variable. A string literal is actually writing out the string in the code. In something like:

let name = 'Dave';

in this 'Dave' is the string literal, you have written it out, exactly as you want it to be. When the code gets compiled, it has to has to create a section of memory just for that, just as you wrote it, or “as it is written”, another way to say “literal”.

When they talk about “literals”, that’s how I think of it in my head. You could also have an object literal:

const myObj = { prop1: 123 };

In this case, { prop1: 123 } is an object literal.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.