JavaScript

JavaScript is a scripting language used to add interactivity to websites. JavaScript is often used in HTML5 canvas games as the main programming language, and more recently as a scripting language for some game engines.

Pros and Cons

JavaScript has increasingly become a beginner's language when it comes to game development with new technologies like HTML5 canvas and game engines like Construct 2 supporting it. However, JavaScript has been criticized over the years due to many design flaws and semantics.

Pros

  • Easy to use and beginner friendly
  • Works on almost any modern GUI browser
  • Dynamically type
  • Multi-paradigm
  • Built-in garbage collection
  • Access to DOM and <canvas> objects allows you to create browser games without any additional libraries or frameworks
  • Built-in JSON parser
  • AJAX allows easy communication with a web server through HTTP(S)

Cons

  • Requires basic HTML and CSS knowledge to fully learn
  • Slow and bloated even compared to other scripting languages
  • Browser differences in JavaScript can cause compatibility issues
  • Heavily depends on global variables
  • Full access to local file system requires additional libraries
  • Since every variable can be anything or become anything at any time, JS isn't able to tell you when you've assigned a wrong type of value to a variable or mistyped the name. It may just keep drudging along even if it's using complete nonsense data (e.g. objects instead of numbers) and referencing variables that don't exist.

Supported tools

Frameworks

Engines

  • Unity - Uses a JavaScript like language called "Unity Script". (Though still called JavaScript for some reason)
  • Construct - Programming is 100% optional but if you do use it, it's JavaScript

Resources

Tutorials

Transcompiled languages

  • CoffeeScript - Uses a more Python inspired syntax
  • LiveScript
  • PureScript - Uses a functional-programming approach
  • Nim - Also uses a Python inspired syntax. Can also compile to C, C++, and Objective-C.

Miscellaneous

  • Electron - Allows you to create desktop applications with JavaScript and HTML5

Sample code

Hello World

Adds a line of text to the body of the HTML document

document.writeln("Hello, world!");

Outputs to the browser's console (usually access via Inspect Element on most browsers)
console.log("Hello, world!");

Creates an alert box
alert("Hello, world!");

FizzBuzz

function fizzBuzz(count) {
    for (var i=0; i<count; i++) {
        if ((i%3) == 0)
            console.log(i, "fizz");
        else if ((i%5) == 0)
            console.log(i, "buzz");
        else
            console.log(i);
    }
}
 
fizzBuzz(16);

Examples of object behavior

Javascript variables and objects are very flexible. They have no particular structure and can be anything and changed in any way at any time.

function Vec3(x, y) {
    this.x = x;
    this.y = y;
    this.z = 0;
 
    this.randomize = function() {
        this.x = Math.random();
        this.y = Math.random();
        this.z = Math.random();
    }
}
 
var something = new Vec3(10, 50);
something.randomize();
 
var inlineobj = {x:10, y:50, z:0};
 
var xspeed = 11.5;
for (var i=0; i<10; i++) {
    something.x += xspeed;
    inlineobj.x += xspeed;
}
 
inlineobj.name = "faggot";
inlineobj.something = 5000;
inlineobj["something"] += 555; // object properties can also be queried like a dictionary
delete inlineobj.something; // objects cannot be "deleted", but their properties can
 
// variables can be freely reassigned to become anything
inlineobj = 154;
inlineobj = ["hello", 1, 2, 3, 500];
inlineobj.push(700); // push item to array
inlineobj = function() {
    console.log("hello world");
};
inlineobj();
inlineobj = new Vec3(8, 16);
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License