Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

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>
share|improve this question
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 Jan 16 '13 at 5:02
    
@AKTed could you explain why i should prefer newline (\n) more than line feed (\r) ? –  abhisekp Jan 16 '13 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 Jan 16 '13 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. –  Lèse majesté Jan 16 '13 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 Jan 17 '13 at 5:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.