A strange quirk of JXA

Open the JXA REPL:

osascript -l JavaScript -i

Try the following commands:

console.log("Hello World") //prints "Hello World" to the console
console.log("Hello World/rBye  ") // prints "Bye    World" to the console.

I’m not sure whether this will ever be useful but it is certainly interesting and I’m not sure what it’s purpose is… It’s not something that is native to JavaScript… Interestingly this feature/bug is only existent with console.log(). Other commands like app.say("Hello World\rBye "), say both “hello world” and “bye”.

\r is “Carriage Return”. Litterly it moves the text output point to the start of the line without moving to the next line.

Hence you see "Bye " overwriting “Hello”

This is why some traditional systems (like DOS and probably Windows) use CR LF pairs for end of line markers rather than just plain CR (as traditional Mac does) or LF (as traditional Unix (and hence modern Mac) does)

Use \n (linefeed) instead.

Note that the handling of \r and \n varies wildly from place to place even on the Mac, and even more so across platforms.

Really?! I never knew that’s what carriage return meant! Makes a lot more sense now. I know from experience in VB6 to create a new line you use vbCrLf - carriage-return line-feed - but never really understood what carriage-return was. Thanks for the info!