what is a JS string literal?
kindly provide a few examples
thanks!
what is a JS string literal?
kindly provide a few examples
thanks!
I don’t have the time to type up a long explanation, but here is a website I found. Hope it helps!
I think this link might be more directly what you were looking for, @obxjuggler
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_types#string_literals
" A string literal is zero or more characters enclosed in double ( "
) or single ( '
) quotation marks. A string must be delimited by quotation marks of the same type (that is, either both single quotation marks, or both double quotation marks)." – MDN
The MDN site is really a great place to look for answers to questions like this, although it might take a few tries to understand how to search on it effectively.
I know I’ve seen a nice simple definition of string literal somewhere, but perhaps it might help you to think that a literal is the actual string instead of a variable containing a string.
A string literal example: ‘telephone’
A string variable set to the string literal ‘telephone’ : let item = ‘telephone’
Does that help? If you’re knew to programming, the terminology can be confusing, but I promise a time will come when it will ‘click’.
Simple defenation is:
A string literal or anonymous string is a type of literal in programming for the representation of a string value within the source code of a computer program.
You embed them in side backticks
Think in terms of characters. It all stems from there. When we define a domain with recognizable delimeters we can string together any number of characters in sequence and create character string objects. We call them string
for simplicity, but should not forget that it is a sequence of characters. Each character represents a data point that any software environment can target and manipulate.
So the key to understanding this aspect is to think in terms of characters and how to combine them in linear sequences. Given this structure we can devise all manner of software to access/manipulate it.
There is one other concern within the JS environment… The casting of string literals to actual String objects.
const str = "literal"
Above, str
is assigned a string literal which is a primitive value with no type. It is literally a character string sequence with quote delimiters.
str
will know where this sequence lives in memory. The sequence itself does not become a String object until we actually poll the variable.
console.log(str)
The .log()
method can only do one thing… Send text representations to the console. It gives all arguments full scrutiny and makes a best effort to convey them as character string representations.
Naturally, strings are the easiest objects to unpack and recast since they don’t need to be cast. Their characters are easy to pick out.