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


Share this comparison:

If you read this far, you should follow us:

"Constant vs Literal." Diffen LLC, n.d. Web. 21 Feb 2019. < >