Discuss Scratch
- Discussion Forums
- » Advanced Topics
- » Python-like syntax in JavaScript?
- INSERT-USER_NAME
-
Scratcher
1000+ posts
Python-like syntax in JavaScript?

The switch statement uses Python-like syntax for it's cases.
This is somewhat confusing me, because why the syntax from a different language all of a sudden?
this is the Python syntax i'm referring to:
if bool: print('bool was true')
switch (cs) { case 'Example...': console.log('Sudden Python syntax?'); }
- ajskateboarder
-
Scratcher
1000+ posts
Python-like syntax in JavaScript?
The Python-like syntax is a coincidence, the syntax for switch-cases in JavaScript actually derives from C (the same syntax is also in Java)
Also, similar syntax can be seen with labelled statements, which are similar to gotos from languages like C or Batch. As for why switch-cases and similar things haven't used braces? No idea whatsoever
Also, similar syntax can be seen with labelled statements, which are similar to gotos from languages like C or Batch. As for why switch-cases and similar things haven't used braces? No idea whatsoever
Last edited by ajskateboarder (May 16, 2024 02:26:01)
- davidtheplatform
-
Scratcher
500+ posts
Python-like syntax in JavaScript?
The Python-like syntax is a coincidence, the syntax for switch-cases in JavaScript actually derives from C (the same syntax is also in Java)Python match cases (not switch cases) are significantly more complex than C-like switch cases. Here's the documentation for them. Basically you can use wildcards and match against a complex data structure, for example matching an array of objects that all have the same value for a variable.
Also, similar syntax can be seen with labelled statements, which are similar to gotos from languages like C or Batch. As for why switch-cases and similar things haven't used braces? No idea whatsoever
- dynamicsofscratch
-
Scratcher
1000+ posts
Python-like syntax in JavaScript?
i'm pretty sure the q was a'ed by @ajskateboarder so i'll ask the op to close this
- ninjaMAR
-
Scratcher
1000+ posts
Python-like syntax in JavaScript?
(#2)tldr; the design practice of not using braces here derives from labels and goto's in C. I'm pretty sure that there aren't brackets used, because the label is part of the switch (imho easier to scope code this way)
The Python-like syntax is a coincidence, the syntax for switch-cases in JavaScript actually derives from C (the same syntax is also in Java)
Also, similar syntax can be seen with labelled statements, which are similar to gotos from languages like C or Batch. As for why switch-cases and similar things haven't used braces? No idea whatsoever
- mybearworld
-
Scratcher
1000+ posts
Python-like syntax in JavaScript?
Also, the indentation doesn't matter at all. The below code works, even though it wouldn't in Python.
switch (cs) { case 'Example...': console.log('Sudden Python syntax?'); console.log("No, indentation doesn't matter"); console.log("foo"); break; }
- INSERT-USER_NAME
-
Scratcher
1000+ posts
Python-like syntax in JavaScript?
i'm pretty sure the q was a'ed by @ajskateboarder so i'll ask the op to close thisQuestion has indeed been answered, and explained a bit too. I'll close this.
- Discussion Forums
- » Advanced Topics
-
» Python-like syntax in JavaScript?