JavaScript Number, String and Boolean Literals
Quick answer
A literal is source notation that directly expresses a value, such as 12, "book", or true. A number represents a numeric value, a string represents text, and a Boolean represents true or false.
How it works
Write numbers without quotes, strings with matching single or double quotes, and Boolean literals as lowercase true or false. The + operator adds two numbers but concatenates two strings. Other combinations and conversions are covered in the types chapter.
console.log(8 + 2);console.log("8" + "2");Expected console output (Node.js):
1082The first result is a number; the second is a string made of two characters.
Expected console output (Node.js):
trueAvailableThe first literal is Boolean, and the second is a string.
Common mistakes
"false" is a string, not the Boolean false. Similar-looking console output does not prove that two values have the same type.
Practice
Predict the results of adding 3 + 4 and "3" + "4", then run both.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
JavaScript Number, String and Boolean Literals — Knowledge Check
Review the concept and apply it to a new example.
Which is a Boolean literal?
What is the result of "3" + "4"?
Which is a number literal?
What is the result of 3 + 4?
Which pair represents text and a Boolean respectively?