JavaScript

Overview

JavaScript was originally developed to determine what browser a web page was loading on. It is commonly referred to as the language which was “designed in a weekend” or “designed in 2 weeks” (both which are partial truths).

The most common use for JavaScript today is to enrich basic HTML webpages, as well as run back-end server tasks with a platform such as node.js. It can also be used to develop desktop applications with a platform such as nw.js or electron.

Child Pages

Immutability
ContentsDeeply Nested ObjectsDeeply Nested Objects When using the Immutable.js library, all access must be done using the  through the get(), getIn(), set() or setIn() family of functions. Like this:Like Loading...
Namespaces
How to get the equivalent of "namespaces" in JavaScript, which does not have built-in support for them.
Variable Scope
Function scope (var), block scope (let), EMCAScript 2015 and more info on variable scope in the JavaScript language.

Adding JavaScript Code In An HTML Page

There are two basic ways to add Javascript code into a web page. You can either: Include a JavaScript file (.js)

Or include the JavaScript code directly into the web page

If adding a standalone javascript file, you can use the keyword defer  to tell the browser to wait until the rest of the page has loaded before loading the Javascript file.

Constants

Constants can’t really be defined in JavaScript, at least not in the way you can in C and C++. One of the easiest workarounds is to just you all-uppercase variable names, like you would in C, so people will know that it shouldn’t be changed.

Chrome let’s you declare constants, but they are not that useful as they are not multi-browser compatible.

Functions

You can return values from a function by using the return keyword.

You can return multiple values easily by creating an object on the fly (this is called an object literal).

 

Message Boxes

A simple message box can be created easily in JavaScript by using the alert() function.

The Console

Many browsers support writing to the console with the command console.log() , in a similar manner that you would use printf for C/C++ in a Linux environment. The added ability of console.log() is that it can be passed most objects, and these will be printed with their own ToString() method.

Strings

Strings can be defined with either the " or '  character. Both are allowed, to distinguish between HTML strings and JavaScript strings when JavaScript is wrapped in HTML. In this case, you need to use the ‘ character for JavaScript strings, and the " character for HTML strings.

String To Number Conversion

You can use the native javascript function parseFloat() .

Note that there is no native parseDouble()  function.

However, be careful! Javascript has some very weird implicit conversion rules, as highlighted below:

Credit for the above goes to https://i.imgur.com/6aclmM6.png.

The Maths Object

Like many languages, JavaScript has a Maths object which you can use to do basic mathematical operations with.

 

Posted: September 15th, 2011 at 7:44 pm
Last Updated on: April 10th, 2017 at 8:36 am