Software programmers use various data types in their code.

A literal is a value that is expressed as itself. For example, the number 25 or the string "Hello World" are both literals.

A constant is a data type that substitutes a literal. Constants are useful in situations where

A variable in a program can change its value during the course of execution of the program. A constant retains the same value throughout the program.

Comparison chart

Constant versus Literal comparison chart
Edit this comparison chartConstantLiteral
Example const PI = 3.14; var radius = 5; var circumference = 2 * PI * radius; var radius = 5; var circumference = 2 * 3.14 * radius;

Constant vs Literal Data Type - Example

Suppose we are writing a program to determine which members of a population are eligible to vote, permitted to drink, both or neither.

const DRINKING_AGE = 21;
const VOTING_AGE = 18;

18 and 21 are literals. We can use these literals in all areas of our program. For example, if(age > 18) or if(age < 21). But we can make our code more understandable if we use constants instead. if(age > VOTING_AGE) is easier to understand. Other benefits of using constants are

References

About the Author

Nick Jasuja

Nick Jasuja has over 15 years of technology industry experience, including at Amazon in Seattle. He is an expert at building websites, developing software programs in PHP and JavaScript, maintaining MySQL and PostgreSQL databases, and running Linux servers for serving high-traffic websites. He has a bachelor's degree in Computer Science & Engineering.

Share this comparison via:

If you read this far, you should follow us:

"Constant vs Literal." Diffen.com. Diffen LLC, n.d. Web. 11 Oct 2025. < >