JavaScript

Overview

JavaScript was originally designed to determine what browser a web page was loading on.

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.

The Maths Object

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

Useful JavaScript Libraries

Listed below are some of the Java apps I have found useful in website design.

Lightbox

Lightbox is a JavaScript app that is used to display images on a web page. It kicks in when the user click on a link to an image and brings up the image into the forground with out ever leaving the original page. It is popular because of its sleek looks, fluid motion, and darkening of the background to highlight the image. There are lots of lightbox versions out there that use different java scripts and have slightly different behaviours. Lightbox 2 is my favourite, which has nice smooth transitions between images and allows for easy grouping of certain images into galleries.

Google Analytics

Google Analytics is a useful tool for recording how many people visit your website. It is geared towards marketing and features powerful analytical tools to analyse the impact marketing and advertising has on your website. It is free and just requires you to sign up and then add a small java script to the header of your webpages.