# Literal

May 20, 2023

A literal is a value that appears directly in a program’s source code, rather than being computed or retrieved from a variable or other memory location. Literals are used to represent fixed values such as numbers, strings, and boolean values.

## Purpose

The purpose of using literals is to provide a simple and efficient way of specifying fixed values without having to store them in memory. This is useful for a variety of programming tasks, such as initializing variables, passing arguments to functions, and defining constants. By using literals, programmers can avoid the overhead of storing and retrieving values from memory, which can improve the performance of their code.

## Usage

Literals are used in many programming languages, including JavaScript, Python, Ruby, and Java. The syntax for defining literals varies depending on the type of literal being used. Here are some examples of common types of literals and their syntax:

### Numeric literals

Numeric literals are used to represent numbers, such as integers, floating-point numbers, and hexadecimal values. In JavaScript, numeric literals can be written in decimal, hexadecimal, or exponential notation. Here are some examples:

``````let a = 42; // decimal notation
let b = 0x2a; // hexadecimal notation
let c = 4.2e+1; // exponential notation
``````

In Python, numeric literals can be written in decimal, binary, octal, or hexadecimal notation. Here are some examples:

``````a = 42 # decimal notation
b = 0b101010 # binary notation
c = 0o52 # octal notation
d = 0x2a # hexadecimal notation
``````

### String literals

String literals are used to represent text, such as words, sentences, and paragraphs. In most programming languages, string literals are enclosed in quotation marks, either single or double. Here are some examples:

``````let a = 'hello'; // single quotes
let b = "world"; // double quotes
let c = `hello \${b}`; // template literals (ES6)
``````

In Python, string literals can be enclosed in either single or double quotes, but triple quotes are also used to represent multi-line strings. Here are some examples:

``````a = 'hello' # single quotes
b = "world" # double quotes
c = '''hello
world''' # triple quotes (multi-line)
``````

### Boolean literals

Boolean literals are used to represent logical values, either true or false. In most programming languages, boolean literals are keywords or symbols. Here are some examples:

``````let a = true; // keyword (JavaScript)
let b = false; // keyword (JavaScript)
let c = 1 < 2; // comparison (JavaScript)
``````

In Python, boolean literals are keywords. Here are some examples:

``````a = True # keyword
b = False # keyword
c = 1 < 2 # comparison
``````

### Null and undefined literals

Null and undefined literals are used to represent the absence of a value or the lack of a defined value. In JavaScript, null and undefined are keywords. Here are some examples:

``````let a = null; // keyword
let b = undefined; // keyword
let c; // undefined (implicitly)
``````

In Python, None is used to represent null or undefined values. Here are some examples:

``````a = None # keyword
b = None # keyword
c = None # implicitly undefined
``````