What is a literal in computer programming?

Thabo Ambrose
2 min readJun 25, 2019

--

A short explanation of what literals are and how to use them.

In my programming life, I have read and heard the term literal being used in written and video tutorials, I did not know what a literal was exactly but I could guess what it was in the context of what I was learning, I do not like guessing that much anymore. Well..yeah.

What is a literal?

In computer programming, a literal is the idea of expressing a non-changing value in a computer program’s source code.

You know, literally the value..

Types and examples

Literals can be of almost any type in a programming language, in fact, a literal can somewhat infer a type(I’ll come back to this later), the types of literals I am aware of are:

  • String .eg “Ambrose
  • Number/Decimal .eg 30
  • Boolean .eg True/False
  • Hexadecimal .eg 0xa1b
  • Anon function/function literal .eg () => “Ambrose”
  • Anon class .eg class {constructor(){}}
  • Object literal .eg {name: “Ambrose”}
  • Enumeration/Enum . {good = GOOD}

Usage

Literals can be passed to variables or an identifier, when declaring a variable, you can initialize the variable with a literal, let’s look at how a literal could be used to initialize a value.

Exampleconst num1 = 30; // 30 is a literal
const str1 = "Ambrose" // Ambrose is a literal
const myObject = {
myClass: class {constructor(){}} // the class is a literal
}
const myFunction = () => "Ambrose" // the anon func is a literal
const myHexValue = 0xa1b

So yeah.. a literal is nothing but a value.

Since I said a literal is the idea of an unchanging value, I am not confusing it with a constant though, a constant uses a literal as it’s value.

Okay that is it, a quick look into what literals are.

Cheers ✌.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response