Learn JavaScript in Construct, part 2: language basics

31

Index

Features on these Courses

Stats

14,294 visits, 37,519 views

Tools

Translations

This tutorial hasn't been translated.

License

This tutorial is licensed under CC BY-NC 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

Published on 24 Sep, 2021. Last updated 16 Dec, 2021

This is part 2 of the tutorial series Learn JavaScript in Construct. This picks up from where part 1 left off. So in case you missed it, see Learn JavaScript in Construct, part 1: getting started.

We'll keep using the same project from the previous part. You can edit the code block in the On start of layout event by double-clicking it, changing the code, and then clicking outside or pressing Ctrl + Enter to finish editing. Then you can preview the project and open the browser console in developer tools to see the results. It's worth getting familiar with this process, as we'll be relying on it a fair bit. Try out the following code snippets this way to see how they work.

Sometimes the code snippets will tell you to try them out in the browser console. In this case don't enter them to your code block in Construct - preview the project, open the browser console, and try typing each line of the code snippet in there instead.

Statements

JavaScript code consists of a series of statements, separated by semicolons ;. For example this code logs two console messages, and each line is a statement:

console.log("First message!");
console.log("Second message!");

You can put two statements on the same line, as it's the semicolon that really separates statements. However conventionally there is a single statement per line.

Comments

In programming, a comment is simply something you can write in your code that the computer will ignore. In other words it's a note for the people reading the code, rather than an instruction for the computer to carry out. This is a similar idea to comments in event sheets, which are also just notes. There are two kinds of comments in JavaScript.

Single-line comments

Anywhere two forward slash characters // appear indicates a single-line comment. Everything from the forward slash characters up to the end of the line is a comment. The comment can occupy its own line or come after some code.

// This is a single-line comment!
console.log("Hello world!");	// And this!

This tutorial guide will often use these kinds of comments to annotate code, indicating what it does or what result some code will produce.

Multi-line comments

Single-line comments naturally end at the end of the line, so if you want a long comment, you need to comment every line:

// First line of comment
// Second line of comment
// Third line of comment

Multi-line comments can be an easier way to write this. Multi-line comments start with a forwards slash and asterisk /* and end with an asterisk and forward slash */.

/* First line of comment
Second line of comment
Third line of comment */

Despite the name, you can also write this kind of comment on a single line. This can be useful as you can still write code after the end of the comment.

/* comment before code */ console.log("Hello world!");
console.log(/* comment inside code */ "Hello world!");

In general, you can put comments anywhere you like and it won't affect the code. As you may have found with event sheets too, adding comments is often a good idea to help yourself or other people understand the code. Professionally written code is often heavily commented to help anyone reading it understand how it works. The best code is clear and easy to understand, not obscure and complicated.

  • 8 Comments

  • Order by
Want to leave a comment? Login or Register an account!
  • My project contains only an 'On start of layout' event block with "let b = "Hello";" in it but when I run the game, the variable 'b' returns as undefined until I manually declare it in the console.

    Is this normal behaviour or am I missing something? This is the case with any variable I make - for some reason I'm not able to declare on runtime through an event block.

    • To be more specific, the console returns:

      "Uncaught ReferenceError: b is not defined

      at <anonymous>:1:1"

        • [-] [+]
        • 1
        • Ashley's avatar
        • Ashley
        • Construct Team Founder
        • 1 points
        • (0 children)

        That's normal - the console basically works in global scope, and it can't access variables declared in other functions (and script blocks in event sheets are actually inside functions), just like how you can't write code in one function that accesses a local variable inside a different function.

  • Congratulations!! Very Clear and Objective in the explanations!!!

  • Thank you Ashley, I've been wanting to learn Javascript for sometime now. I tried another console but it was to hard to setup. Using Construct makes it simple. I am not a full time code developer, but the first language I learned on was basic using a mainframe at the Community College way back when. I am a retired teacher working with an English professor developing his English course for online learning. I'm looking forward to implementing the JavaScript in my project to make easier to develop and make it efficient.

  • You might consider including "scope" in the "VAR" section and why "var" is quirky.

      • [-] [+]
      • 3
      • Ashley's avatar
      • Ashley
      • Construct Team Founder
      • 3 points
      • (1 child)

      As explained in part 1 this tutorial isn't intended to go in to detail about the quirks of old legacy features. If you stick to let/const then you don't need to learn about all those quirks.