Open Source For You — December 2017

(Steven Felgate) #1
74 | DECEMBER 2017 | OPEN SOURCE FOR YOU | http://www.OpenSourceForU.com

Developers Insight


World’ script hello.html in JavaScript:

<html>
<body>
<script>
alert(‘Hello World’);
</script>
</body>
</html>

Now let us try to understand the code. The HTML part of
the code is straightforward and needs no explanation. All the
JavaScript code should be placed within the <script> tags
(<script> and </script>). In this example, the following code
uses the alert( ) function to display the message ‘Hello World’
in a dialogue box:

alert(‘Hello World’);

To view the effect of the JavaScript code, open the file
using any Web browser. I have used Mozilla Firefox for this
purpose. Figure 2 shows the output of the file hello.html.
Please note that a file containing JavaScript code alone can
have the extension .js, whereas an HTML file with embedded
JavaScript code will have the extension .html or .htm.

Regular expressions in JavaScript
There are many different flavours of regular expressions
used by various programming languages. In this series we
have discussed two of the very popular regular expression
styles. The Perl Compatible Regular Expressions (PCRE)
style is very popular, and we have seen regular expressions
in this style being used when we discussed the programming
languages Python, Perl and PHP in some of the previous
articles in this series. But we have also discussed the
ECMAScript style of regular expressions when we discussed
regular expressions in C++. If you refer to that article on

Standalone JavaScript applications
Now that we have some idea about the scope and evolution
of JavaScript, the next obvious question is, can it be used to
develop standalone applications rather than only being used
as an embedded scripting language inside HTML scripts?
Well, anything is possible with computers and yes, JavaScript
can be used to develop standalone applications. But whether
it is a good idea to do so or not is debatable. Anyway, there
are many different JavaScript shells that allow you to run
JavaScript code snippets directly. But, most often, this is
done during testing and not for developing useful standalone
JavaScript applications.
Like standalone PHP applications, standalone JavaScript
applications are also not very popular because there are
other programming languages more suitable for developing
standalone applications. JSDB, JLS, JSPL, etc, are some
JavaScript shells that will allow you to run standalone
JavaScript applications. But I will use Node.js, which I
mentioned earlier, to run our standalone JavaScript file first.js
with the following single line of code:

console.log(‘This is a stand-alone application’);

Open a terminal in the same directory containing the file
first.js and execute the following command:

node -v

This will make sure that Node.js is installed in your
system. If Node.js is not installed, install it and execute the
following command:

node first.js

...at the terminal to run the script:

first.js

The message ‘This is a stand-alone application’ is
displayed on the terminal. Figure 1 shows the output of the
script first.js. This and all the other scripts discussed in this
article can be downloaded from opensourceforu.com/article_
source_code/December17Javascript.zip

Hello World in JavaScript
Whenever someone discusses programming languages, it is
customary to begin with ‘Hello World’ programs; so let us
not change that tradition. The code below shows the ‘Hello

Figure 1: Standalone application in JavaScript

Figure 2: Hello World in JavaScript
Free download pdf