What does \n mean in programming?
\n is an escape sequence that represents a newline character (line feed) in most programming languages including JavaScript, Python, Java, C, C++, and PHP. When a program encounters \n in a string, it inserts an actual line break at that position. The backslash (\) tells the interpreter or compiler that the next character has a special meaning rather than being a literal letter 'n'. Other common escape sequences include \t (tab), \r (carriage return), and \\ (literal backslash).
How do you convert newlines to \n?
To convert actual line breaks to the \n escape sequence, replace each newline character with the two-character literal string '\n'. In JavaScript, use text.replace(/\n/g, '\\n'). In Python, use text.replace('\n', '\\n'). This tool performs the conversion automatically: paste your multi-line text and it outputs a single-line string with \n wherever line breaks appeared. This is essential when embedding multi-line content in JSON strings or single-line code assignments.
What is the difference between \n and \r\n?
\n (Line Feed, LF) and \r\n (Carriage Return + Line Feed, CRLF) are different line ending conventions used by different operating systems. Unix, Linux, and macOS use \n (LF) as the line ending. Windows uses \r\n (CRLF). The old Mac OS (pre-OS X) used \r (CR) alone. \r moves the cursor to the beginning of the line, while \n moves it to the next line. Most modern text editors and programming languages handle both formats, but mismatches can cause issues in scripts, Git diffs, and file parsing.
Why do JSON strings need escaped newlines?
JSON specification (RFC 8259) requires that strings be contained on a single line. Literal newline characters inside a JSON string value are invalid and will cause parsing errors. To include a line break in a JSON string, you must use the escape sequence \n. For example, {"text": "line one\nline two"} is valid JSON, but having an actual line break between 'line one' and 'line two' within the quotes would be a syntax error. This is why converting newlines to \n is essential when preparing text for JSON payloads.
How do you convert \n back to actual newlines?
To convert \n escape sequences back to actual line breaks, replace the two-character literal string '\n' with a real newline character. In JavaScript, use text.replace(/\\n/g, '\n'). In Python, use text.replace('\\n', '\n'). This tool also supports the reverse conversion: paste a single-line string containing \n sequences and convert them back to multi-line text with real line breaks. This is useful when reading escaped strings from logs, API responses, or configuration files.
What is a line ending?
A line ending (also called line break, newline, or end-of-line) is a special character or sequence of characters that marks the end of a line of text. The three main types are: LF (\n, hex 0x0A) used by Unix/Linux/macOS, CRLF (\r\n, hex 0x0D 0x0A) used by Windows, and CR (\r, hex 0x0D) used by classic Mac OS. Line endings are invisible characters that control how text wraps in files and terminals. Git can be configured to automatically convert line endings between platforms using the core.autocrlf setting.
What is the difference between LF and CRLF?
LF (Line Feed, \n) is a single character (byte 0x0A) used as the line ending on Unix, Linux, and macOS. CRLF (Carriage Return + Line Feed, \r\n) is a two-character sequence (bytes 0x0D 0x0A) used on Windows. The terms originate from typewriters: carriage return moves the print head to the start of the line, and line feed advances the paper one line. Mixing LF and CRLF in the same file can cause display issues, script failures, and noisy Git diffs. Most modern editors can convert between the two formats.