JavaScript Octal and Binary Literals: A Complete Tutorial with Examples

JavaScript supports octal and binary literals for working with numbers in octal (base-8) and binary (base-2) formats. This can be helpful when dealing with certain types of numerical data, such as file permissions, binary data, bitwise operations, and low-level system programming.

In this tutorial, we will explore how to use octal and binary literals in JavaScript with several code examples.

1. Octal Literals

An octal literal is a number represented in base-8. In JavaScript, octal literals use a leading 0o or 0O (zero followed by a lowercase or uppercase “O”).

Syntax

let octalNumber = 0o[number];

The 0o prefix indicates that the number is in octal format.
[number] is a sequence of digits from 0 to 7.

Example 1: Basic Octal Literals

let octal1 = 0o10; // Octal 10 (base 8)
let octal2 = 0o77; // Octal 77 (base 8)

console.log(octal1); // Output: 8 (decimal)
console.log(octal2); // Output: 63 (decimal)

Explanation

0o10 represents the octal number 10, which equals 8 in decimal.
0o77 represents the octal number 77, which equals 63 in decimal.

Example 2: Using Octal Literals for Permissions

Octal literals are often used in programming environments to represent file permissions, as in Unix-based systems.

let readWriteExecute = 0o777; // Full permissions
let readOnly = 0o444; // Read-only permissions

console.log(readWriteExecute); // Output: 511 (decimal)
console.log(readOnly); // Output: 292 (decimal)

Explanation

0o777 represents full permissions (rwxrwxrwx), which converts to 511 in decimal.
0o444 represents read-only permissions (r–r–r–), which converts to 292 in decimal.

Example 3: Invalid Octal Literals

Octal literals can only contain digits from 0 to 7. Using digits outside this range will result in a syntax error.

// let invalidOctal = 0o89; // SyntaxError: Invalid or unexpected token

Explanation

Octal numbers can only have digits from 0 to 7. Including 8 or 9 in an octal literal will cause an error.

2. Binary Literals

A binary literal is a number represented in base-2. In JavaScript, binary literals use a leading 0b or 0B (zero followed by a lowercase or uppercase “B”).

Syntax

let binaryNumber = 0b[number];

The 0b prefix indicates that the number is in binary format.
[number] is a sequence of 0s and 1s.

Example 4: Basic Binary Literals

let binary1 = 0b1010; // Binary 1010 (base 2)
let binary2 = 0b1111; // Binary 1111 (base 2)

console.log(binary1); // Output: 10 (decimal)
console.log(binary2); // Output: 15 (decimal)

Explanation

0b1010 represents the binary number 1010, which equals 10 in decimal.
0b1111 represents the binary number 1111, which equals 15 in decimal.

Example 5: Using Binary Literals in Bitwise Operations

Binary literals are commonly used in bitwise operations, which operate directly on binary digits of integers.

let mask = 0b0101; // Binary 0101 (decimal 5)
let number = 0b1010; // Binary 1010 (decimal 10)

let result = number & mask; // Bitwise AND operation

console.log(result.toString(2)); // Output: 0 (binary) since there are no matching '1's

Explanation

0b0101 and 0b1010 are binary numbers.
The & (bitwise AND) operation compares each bit and returns 1 if both bits are 1. In this case, the result is 0.

Example 6: Invalid Binary Literals

Binary literals can only contain 0s and 1s. Using other digits will result in a syntax error.

// let invalidBinary = 0b102; // SyntaxError: Invalid or unexpected token

Explanation

Binary numbers can only have digits 0 and 1. Including any other digits in a binary literal will cause an error.

3. Conversions Between Binary, Octal, and Decimal

JavaScript provides built-in methods to convert numbers between binary, octal, and decimal.

Example 7: Converting Decimal to Binary and Octal

You can use the toString() method to convert a number to binary or octal format.

let number = 42;

let binary = number.toString(2); // Convert to binary
let octal = number.toString(8); // Convert to octal

console.log(binary); // Output: "101010"
console.log(octal); // Output: "52"

Explanation

number.toString(2) converts the decimal number 42 to binary (“101010”).
number.toString(8) converts the decimal number 42 to octal (“52”).

Example 8: Parsing Binary and Octal Strings

You can use parseInt() to convert a binary or octal string to a decimal number.

let binaryString = '1010';
let octalString = '52';

let binaryNumber = parseInt(binaryString, 2); // Parse as binary
let octalNumber = parseInt(octalString, 8); // Parse as octal

console.log(binaryNumber); // Output: 10
console.log(octalNumber); // Output: 42

Explanation

parseInt(binaryString, 2) converts the binary string “1010” to its decimal equivalent (10).
parseInt(octalString, 8) converts the octal string “52” to its decimal equivalent (42).

4. Real-world Applications of Binary and Octal Literals

Example 9: Setting Permissions with Octal Literals

Octal literals are useful when setting file permissions, commonly used in Unix-like operating systems.

let readWriteExecute = 0o755; // rwxr-xr-x
let readWriteOnly = 0o644; // rw-r--r--

console.log(readWriteExecute); // Output: 493
console.log(readWriteOnly); // Output: 420

Example 10: Using Binary for Flag Management

Binary literals are often used in low-level programming for flag management.

let FLAG_READ = 0b0001; // 1
let FLAG_WRITE = 0b0010; // 2
let FLAG_EXECUTE = 0b0100; // 4

// Combining flags using bitwise OR
let permissions = FLAG_READ | FLAG_WRITE;

console.log(permissions.toString(2)); // Output: "11" (binary)

Explanation

This example uses binary literals to define flags and combines them using the bitwise OR (|) operation.

 

Summary

JavaScript's support for octal (0o) and binary (0b) literals makes it easier to work with non-decimal numeric systems, which can be helpful in various programming scenarios, such as file permissions and low-level bitwise operations.

Quick Reference

Format Prefix Example Decimal Equivalent
Octal 0o 0o10 8
Binary 0b 0b1010 10

Common Uses

  • Octal Literals: Useful for file permissions and other numeric codes in systems programming.
  • Binary Literals: Helpful for bitwise operations, flag management, and working with binary data.

By mastering these literals, you can write more expressive and understandable JavaScript code when dealing with different numeric systems.

Related posts

JavaScript Operator Precedence

JavaScript Reserved Keywords

JavaScript Syntax Tutorial with Examples