{Tips for Javascript} K&R vs Allman style of writing code. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 28

{Tips for Javascript} K&R vs Allman style of writing code.

In JavaScript the result of a method can depend upon the style braces are placed. This is the K&R style, where braces are placed right after the method signature and after a return statement: var foo = function() { return { key: 'value' }; } foo() // returns an object here Now, if I format this code to the Allman style, where braces are always placed on a new line, the result is different: var foo = function() { return { key: 'value' }; } foo() // returns undefined here How come? In JavaScript the language places automatically semicolons at the end of each line if you won't do it yourself. So what really happened in the last code fragment was this: var foo = function() { return; // here's actually a semicolon, automatically set by JavaScript! { key: 'value' }; } So if you'd call foo(), the first statement in the method would be a return statement, which would return undefined and would not execute other following statements. Source: Stackoverflow

4th Mar 2017, 4:53 AM
Ram chandra Giri
Ram chandra Giri - avatar
4 Answers
+ 22
@Jafca that was not the only reason now will not be able to use solo learn that much cause our exam is coming and these three physics are giving me headache. @Luka thank you so much.
4th Mar 2017, 12:29 PM
Ram chandra Giri
Ram chandra Giri - avatar
+ 21
thanks I will open solo learn everyday though.
4th Mar 2017, 12:32 PM
Ram chandra Giri
Ram chandra Giri - avatar
+ 17
Interesting You going for Question Guru or something? 😏
4th Mar 2017, 5:26 AM
Jafca
Jafca - avatar
+ 13
@Ram chandra Giri Good luck, man. All the best 🍀
4th Mar 2017, 12:31 PM
Jafca
Jafca - avatar