Discuss Scratch

MegaApuTurkUltra
Scratcher
1000+ posts

Scratch JS Tutorial Forum

AmazingMech2418 wrote:

Then why is typescript not available on all devices? JavaScript is a great language.
You know what else is available on all devices?

C
MollTheCoder
Scratcher
6 posts

Scratch JS Tutorial Forum

AmazingMech2418 wrote:

JS basics #1

JavaScript is a common programming language for web and game programming. It includes variables and functions. It is case sensitive.
You define variables like this:
var a = "hi";
You have to put the semicolon at the end.
You define functions like this:
function myFunction() {
code
}
or
var myFunction = function() {
code
}
There are also alert boxes that use the alert() function, but that will be described more in JS basics #2.
You do not need semicolons for everything, but some things might need them. JavaScript has an automatic semicolon system. But, some people consider it good practice to add semicolons.
Sheep_maker
Scratcher
1000+ posts

Scratch JS Tutorial Forum

Apart from the three statement for loop, semicolons also obligatory before lines that start with (, `, [, and *.

const four = 4
 
(async () => {
  await confirmIsFour(four)
})()
JavaScript will assume that you're trying to call 4 as a function, with the async arrow function as a parameter, which will throw an error since 4 is not a function

const four = 4
 
`Four is also written as ${four}`.split('').forEach(char => {
  console.log(`**${char}** `)
})
JavaScript thinks that you're calling a tagged template literal, but since 4 isn't a function, it'll throw an error

const four = 4
 
[1, 2, 3, four].forEach(num => {
  console.log(num * num)
})
JavaScript assumes that you're trying to get a property of 4. It'll try to get 4[4] since the comma operator returns the final value, and since it's undefined, trying to get .forEach will throw an error.

class Four {
  value = 4
 
  * generateFourFours () {
    yield this.value
    yield this.value
    yield this.value
    yield this.value
  }
}
JavaScript thinks you're trying to multiply 4 in the class field by some value rather than interpreting the asterisk as an indication of a generator function, but it throws a syntax error elsewhere because a curly bracket doesn't make sense when following a function call

Standard requires a semicolon before each line starting with those characters to prevent this except for the last case since class fields are fairly new

Powered by DjangoBB