JavaScript literals are fixed values that you literally provide in your script. These values can be numbers, strings, objects, arrays, booleans, null, or even regular expressions. This guide will cover each type of literal in detail, providing examples for better understanding.
1. Numeric Literals
Decimal (Base 10):
Binary (Base 2):
Octal (Base 8):
Hexadecimal (Base 16):
2. String Literals
Single Quotes:
Double Quotes:
Template Literals:
let templateString = `Hello, ${name}!`;
3. Boolean Literals
True:
False:
4. Null Literal
5. Undefined Literal
console.log(notAssigned); // undefined
6. Object Literals
firstName: 'John',
lastName: 'Doe',
age: 30
};
You can also include methods in object literals:
let personWithMethod = {
firstName: 'Jane',
lastName: 'Doe',
greet: function() {
return `Hello, ${this.firstName} ${this.lastName}!`;
}
};
7. Array Literals
Arrays can contain mixed types of values:
8. Regular Expression Literals
You can also include flags:
9. Function Literals
return a + b;
};
ES6 introduced arrow functions which provide a more concise syntax for function literals:
10. BigInt Literals
11. Symbol Literals
Examples in Context
// Numeric Literals
let age = 25;
let binaryAge = 0b11001;
let hexAge = 0x19;
let octalAge = 0o31;
// String Literals
let firstName = "John";
let lastName = 'Doe';
let fullName = `${firstName} ${lastName}`;
// Boolean Literals
let isAdult = true;
let isStudent = false;
// Null and Undefined Literals
let job = null;
let car; // undefined
// Object Literal
let person = {
firstName: "Jane",
lastName: "Doe",
age: 28,
isEmployed: true,
greet: function() {
return `Hi, I'm ${this.firstName} ${this.lastName}.`;
}
};
// Array Literal
let hobbies = ['Reading', 'Cycling', 'Hiking'];
// Regular Expression Literal
let emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
// Function Literals
let greet = function(name) {
return `Hello, ${name}!`;
};
let greetArrow = name => `Hello, ${name}!`;
// BigInt Literal
let bigNumber = 1234567890123456789012345678901234567890n;
// Symbol Literal
let uniqueID = Symbol('id');
// Logging examples
console.log(age, binaryAge, hexAge, octalAge); // 25 25 25 25
console.log(firstName, lastName, fullName); // John Doe John Doe
console.log(isAdult, isStudent); // true false
console.log(job); // null
console.log(car); // undefined
console.log(person.greet()); // Hi, I'm Jane Doe.
console.log(hobbies); // ['Reading', 'Cycling', 'Hiking']
console.log(emailPattern.test('test@example.com')); // true
console.log(greet('Alice')); // Hello, Alice!
console.log(greetArrow('Bob')); // Hello, Bob!
console.log(bigNumber); // 1234567890123456789012345678901234567890n
console.log(uniqueID); // Symbol(id)
JavaScript literals are a fundamental part of the language, allowing you to represent data in various forms directly in your code. Understanding and utilizing these literals effectively can greatly enhance your ability to write concise and readable JavaScript programs. From simple numeric and string literals to complex object and function literals, each type serves a unique purpose in making JavaScript a powerful and flexible language.
Practice Excercise Practice now