JavaScript Crash Course
Let's master JavaScript from a single article. This crash course will bring us to code basic JavaScript. Feel free to bookmark this article as your mini note of JavaScript.
JavaScript is a programming language that adds interactivity to your website. Not only for websites. We also can make mobile apps, back-ends, and many more. It looks like it tries to be a general-purpose programming language. It was invented by Brendan Eich, a co-founder of Mozilla.
The only programming language that runs on the web browser is JavaScript. You can open the developer console to your web browser, open the Console tab, then type console.log("Hello World!");
. Congratulations. We just made a "Hello World!".
Prerequisites
We need to install some prerequisites before we can start coding.
- Text editor. I suggest Microsoft Visual Studio Code with some extensions such as Live Preview from Microsoft. So, we can see the HTML result on our text editors.
- NodeJS. I suggest installing the LTS version. You can try Node Version Manager to manage NodeJS versions easily. So, we can run the JavaScript on the terminal.
There are Web APIs from web browsers and NodeJS APIs from NodeJS that run on JavaScript. We don't talk about them in this article. Maybe, I will make them in the next articles. Here, we focus on the basic syntax of JavaScript.
Hello World!
|
|
Create a JavaScript file named main.js
with the code above. We created a comment with //
, set "Hello World!" as a heading, and showed up the message as an alert.
|
|
Create an HTML file named index.html
that loads that main.js
. Create an empty <h1>
tag. Open it on the web browser and see the result.
We just did a Document Object Model (DOM) manipulation. We put a content to the <h1>
tag. We also made an alert to the web browser, a typical message of web browsers.
|
|
Create another file named node.js
. Type node node.js
on your terminal. A message will appear there.
Congratulations. We just did a "Hello World!". Let's go to syntaxes.
Variables
|
|
The var
is the only native or original JavaScript syntax to define a variable. It runs on old web browsers.
- The
var
can redeclare. - The
var
can be out of scope. - A variable can be defined without a
var
.
Let
|
|
The let
was introduced in ES6 (2015).
- The
let
cannot redeclare. - The
let
must be declared before use. - The
let
has a block scope.
Const
|
|
The const
was introduced in ES6 (2015).
- The
const
cannot redeclare. - The
const
cannot reassign. - The
const
has a block scope.
Strings
|
|
Some ways to define strings with its methods.
Numbers
|
|
Integer or float is based on what we defined. The number + text
will do concatenation instead of being calculated. We need to cast the text
to be a number using a Number
before calculating it. The Number
also has some constants such as a maximum or minimum number or integer.
Booleans
|
|
The no
is undefined
. It will turn out to be false if we convert it to be a boolean. The same happens on number 0 too.
Arrays
|
|
We can define arrays by using []
and Array()
. It also has some useful methods such as sort
.
Objects
|
|
Objects are the same as arrays. The difference is the key could be a string for objects. Objects also can contain functions or methods.
Operators
|
|
The ===
in JavaScript is to check the data type. There are also some math and bitwise operators. More about Bit Manipulation or Bitwise Operations can be found here.
Conditions
|
|
Standard flow controls on many programming languages also can be found on JavaScript.
Loopings
|
|
Loopings can happen with while
, do-while
, and for
. The do-while
means it will execute at least once. The for-in
will cast the index. The for-of
will cast the value.
Sets
|
|
The Set
is a collection of unique values. Mostly used to check unique values in fundamental programming.
Maps
|
|
The Map
is a collection of key-value pairs. Mostly used to count values in fundamental programming.
Classes
|
|
Classes were introduced in ES6 (2015). Technically, it is a template for objects.
Closing
JavaScript isn't strict. Even the semicolon is optional. But it is a popular programming language and easy to learn. It has many APIs and libraries. Learning the basics of JavaScript will make a great leap to its ecosystem. The example source code can be found here, https://github.com/aristorinjuang/js-crash-course.
References
Related Articles
- Concurrency in Go (Part 2)
- Deploy a Go App to AWS EC2
- A Simple Blog with Go
- Strapi 4
- React with TypeScript