JavaScript
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.
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.