Skip to content

Latest commit

 

History

History
86 lines (56 loc) · 5.27 KB

File metadata and controls

86 lines (56 loc) · 5.27 KB

JavaScript introduction

ES Logo

📖 Deeper dive reading:

Officially known as ECMAScript, JavaScript is a weakly typed language based upon concepts found in C, Java, and Scheme. It is by far the most used programming language in the world. It runs on every web browser, and it is commonly used as a web server language and for creating serverless functions. In this instruction we will cover the basic parts of the language necessary to create a reasonable website. There are many features of the language that will not be discussed, and you should take time to dig into the corners of the language as time allows. The more effectively you understand JavaScript, the better web programmer you will be.

Typically JavaScript is executed using an interpreter at runtime instead of compiling it into a machine specific binary at build time. This has the advantage of making JavaScript very portable, but also allows for many errors, such as using an undefined variable. These types of errors commonly only get discovered when the program crashes during execution.

JavaScript Versions

The following table describes the version history of JavaScript. You don't need to worry too much about versions right now, but this is important to be aware of since browser compatibility is always an issue when developing a web application. When considering the use of a JavaScript feature you should consult websites like MDN or CanIUse to see how well the feature is supported.

Year Version Features
1997 ES1 types, functions
1999 ES3 regex, exceptions, switch
2009 ES5 json, array iteration
2015 ES6 let/const, default params, classes, template literals, destructuring, generators, promises, modules, internationalization
2016 ES2016 array.includes
2017 ES2017 async/await
2018 ES2018 rest/spread, promise.finally
2019 ES2019 string.trim
2020 ES2020 ?? operator

Getting started

Let's start with a basic example. The following JavaScript will concatenate three strings together and then throw away the result. Not very useful, but JavaScript doesn't complain much.

'Hello' + ' ' + 'world';

Only slightly more complex is to call a function with the result of our concatenated string. In this case we call the JavaScript runtime's built in function console.log to output the string to the debugger console.

console.log('Hello' + ' ' + 'world');
// OUTPUT: Hello world

You can also write your own functions.

function join(a, b) {
  return a + ' ' + b;
}

console.log(join('Hello', 'world'));
// OUTPUT: Hello world

Comments

You can comment your JavaScript with either line or block comments.

// Line comment

/*
Block comment
*/

Code delimiters

While not technically required in most cases, it is considered good form to end JavaScript statements with a semicolon (;). Code blocks, and their resulting scope, are defined with curly braces ({ }).

Playgrounds

Before we go any further we need a way for you to write and run JavaScript yourself. There are two easy ways to do this.

  1. Use an online sandbox like CodePen. With CodePen you can write whatever JavaScript you would like and immediately see the results. Make sure you display the CodePen's Console window if your JavaScript is using the console.log function.

    Browser Debugger

  2. Use your browser's debugger. For example, if you open Chrome and press F12 the debugger will display. Select the Console menu option. This will display a JavaScript interpreter where you can write and execute your code.

    Browser Debugger

Examples

You can see examples of all the JavaScript we will talk about by using this HTML page.