Whitespace

garanews's avatar
Published Jun 16, 2022Updated Oct 26, 2022
Contribute to Docs

In programming, whitespace uses horizontal and vertical space characters or blank lines to format code and improve readability.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours

Example

The code example below is hard to read since there is little to no whitespace. It also does not have indentation with spaces so people can not easily see what is nested within what.

function createRangeIterator(min = 0, max = Infinity, step = 1) {
let nextNum = min;
let numCount = 0;
const rangeIterator = {
next: function () {
let result;
if (nextNum < max) {
result = { value: nextNum, done: false };
nextNum += step;
numCount++;
return result;
}
return { value: numCount, done: true };
},
};
return rangeIterator;
}

When whitespace is used, the code is much more readable:

function createRangeIterator(min = 0, max = Infinity, step = 1) {
let nextNum = min;
let numCount = 0;
const rangeIterator = {
next: function () {
let result;
if (nextNum < max) {
result = { value: nextNum, done: false };
nextNum += step;
numCount++;
return result;
}
return { value: numCount, done: true };
},
};
return rangeIterator;
}

All contributors

Contribute to Docs

Learn General on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours