Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
35
rated 0 times [  41] [ 6]  / answers: 1 / hits: 83318  / 9 Years ago, tue, july 7, 2015, 12:00:00

In Python:



print [1,2], 'n', [3,4]


would print



[1,2]
[3,4]


In Javascript:



console.log([1,2],'n',[3,4])


prints



[1,2] 'n' [3,4]


What is the equivalent Javascript statement to the above Python print?


More From » console.log

 Answers
39

You are sending three arguments to console.log



console.log([1,2],'n',[3,4])


Because the first argument, [1,2] doesn't contain formatting elements (e.g. %d), each argument is passed through util.inspect(). util.inspect is returning the string representation of 'n' which is not what you want. You want 'n' to be interpreted.



One solution is to concatenate all arguments into one string



> console.log([1,2]+'n'+[3,4]);
1,2
3,4


Another is to use formatting elements as placeholders, which util.format will substitute with two array's converted values.



> console.log('%sn%s', [1,2], [3,4]);
1,2
3,4


I assumed node.js here, but the result in Mozilla is identical.


[#65905] Friday, July 3, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dillionsalvadorg

Total Points: 288
Total Questions: 103
Total Answers: 75

Location: South Korea
Member since Sat, Oct 2, 2021
3 Years ago
;