Discuss Scratch

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')
And this is case syntax:
switch (cs) {
case 'Example...':
  console.log('Sudden Python syntax?');
}
I'm sure there's meaning and reason behind this, but i'm still confused by this.
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

Last edited by ajskateboarder (May 16, 2024 02:26:01)

davidtheplatform
Scratcher
500+ posts

Python-like syntax in JavaScript?

ajskateboarder wrote:

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
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.
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?

ajskateboarder wrote:

(#2)
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
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)
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?

dynamicsofscratch wrote:

i'm pretty sure the q was a'ed by @ajskateboarder so i'll ask the op to close this
Question has indeed been answered, and explained a bit too. I'll close this.

Powered by DjangoBB