1

In javascript, Line Feed and Carriage Return behaves similarly.

Which one to use and when?

OR

Can it be used alternatively?

Unicode Characters:

Line Feed: \u000A

Carriage Return: \u000D

Code:

<script>
    alert("Hello \u000A World - <LF>");
    alert("Hello \u000D World - <CR>");
</script>
8
  • 1
    While not exactly an answer to your Question, the newline (\n) is preferable, at least in your example (and probably elsewhere as well).
    – akTed
    Commented Jan 16, 2013 at 5:02
  • @AKTed could you explain why i should prefer newline (\n) more than line feed (\r) ?
    – abhisekp
    Commented Jan 16, 2013 at 5:22
  • 1
    I shouldn't have said preferable since that implies a standard. It is really a lazy way to code. See this Answer for why you should really use both: /r/n.
    – akTed
    Commented Jan 16, 2013 at 5:51
  • 2
    @AKTed: That question and answer is about the HTTP protocol. If you're writing an HTTP client in JavaScript, then, sure, you should use \r\n, but that's a very specific context. In the context of an alert box and most other scenarios where you just want to create a new line, all you need is \n ("newline"). There's no real reason to use a carriage return. There's no carriage or cursor to return or an arbitrary specification/outdated tradition that requires it. Commented Jan 16, 2013 at 8:16
  • 1
    You can write "\n" in an alert. alert("hello" + "\n" + "world") or simply alert("hello\nworld"). As far as I know, that's valid in all browsers. I can't say which is better, but that's the way I've usually seen/done it.
    – akTed
    Commented Jan 17, 2013 at 5:23

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.