What Is JavaScript?

What Is JavaScript

Date First Published: 20th July 2022

Topic: Web Design & Development

Subtopic: Web Development

Article Type: Computer Terms & Definitions

Difficulty: Medium

Difficulty Level: 4/10

Learn more about what JavaScript is in this article.

JavaScript is a client-side programming language used to create interactive effects and dynamic elements within web browsers. It was invented by Brendan Eich in 1995 whilst he was at Netscape Communications Corporation and became an ECMA standard in 1997. The official name of the standard is ECMA-262 and ECMAScript is what JavaScript is based on, but that name, sounding like a skin condition, is not widely used and most people will always call the scripting language 'JavaScript'. JavaScript was originally called 'LiveScript' but was later renamed to resemble the popularity of Sun Microsystem's Java programming language.

In order for JavaScript to run, a web browser with JavaScript enabled is required. If JavaScript is disabled, no JavaScript can run in the web browser, which may break the functionality of most websites. HTML and CSS are used to define the layout, structure, and style of webpages and JavaScript is used to program the behaviour of them.

Note: Info Icon

The official spelling is 'JavaScript' with the camel case 'S'. JavaScript is a brand name owned by Oracle Corporation.


Note: Info Icon

Because JavaScript is a client-side programming language, the script is processed by the client's web browser instead of the web server and can run after a webpage has been loaded without communicating with the web server.

How Can JavaScript Be Included On A Webpage?

JavaScript can either be included internally or externally.

Internal JavaScript

In internal JavaScript, the script is included in the <head> or <body> section of the HTML document. It does not matter where it is included and there is no limit on how many scripts are placed in a HTML document. Internal JavaScript is often used when every webpage needs to use a different code. In the HTML document below, the script is placed between the <script> and </script> tags. An example of a HTML document with a JavaScript function can be seen below.

<!DOCTYPE html> <html> <head> <title>Simple JavaScript Example</title> </head> <body> <h1>Simple JavaScript Example</h1> <p>Below is an example of a simple JavaScript function that divides two numbers.</p> <script> alert(300 / 5) </script> </body> </html>

The output of the HTML document with the JavaScript code can be seen below.

JavaScript Example Screenshot

External JavaScript

A HTML document can link to an external JavaScript file which has the '.js' file extension and can be edited in a text editor, such as Notepad. Multiple HTML documents can use the same JavaScript code and it is useful for large websites with thousands of pages that have the same JavaScript functionality on every webpage as the behaviour of a whole website can be modified just by editing one file.

The path of the JavaScript file needs to be specified. For example, if a file called ‘comment-logic.js' was in a folder called 'js', the correct path would be '/js/comment-logic.js'. Most JavaScript files are linked to every page of a website for the purposes of consistency. An example of a link to an external JavaScript file can be seen below. Several script tags can be added to a HTML document to add more than one JavaScript file.

<script src="comment-logic.js"></script>

Note: Info Icon

External JavaScript files with the '.js' file extension cannot have the <script> tags. Adding those tags will lead to syntax errors and break the functionality of the JavaScript code. Those tags are reserved for use in HTML documents.

Older examples of JavaScript code may use the type attribute below:

<script type="text/javascript">

Because JavaScript is the default scripting language in HTML, this attribute is no longer required with the release of HTML5. However, adding it will not break the functionality of the JavaScript code.

The advantages of adding JavaScript to a webpage by external linking are:
  • It achieves consistency. On large websites, consistency is more difficult to achieve since not every page can be monitored. One external JavaScript file controlling the code of every webpage will ensure that it remains consistent on every page.
  • It is less time-consuming - Even for a website with only a few pages, editing every HTML document just to change the JavaScript code of a website will take a long period of time and it increases the chances of mistakes.
  • The page loading time is improved as the JavaScript file is downloaded once and applied to relevant pages when necessary. The JavaScript file can be cached in order to improve the loading speed of webpages.
The disadvantages of adding JavaScript to a webpage by external linking are:
  • It is not well-designed for webpages that need to have a unique JavaScript code on every page. One JavaScript file that is linked to every HTML page does not allow separate pages to have a different code.
  • Changes made to the external JavaScript file can take longer to take effect for all visitors of the website as the JavaScript file is usually cached and the cache takes some time to expire.
Note: Info Icon

JavaScript accepts both double and single quotes. For example, alert ("This is an alert box.") will perform exactly the same function as alert ('This is an alert box.').

What Can JavaScript Be Used For?

JavaScript can be used for almost anything on a website, including:

  • Validating forms to ensure that the user has not left any fields blank, has inputted a valid email address, and more.
  • Setting, deleting and reading cookies within a web browser to remember information about the user.
  • Performing a range of calculations, including addition, multiplication, subtraction, division, and more.
  • Dynamically modifying HTML and CSS to update the interface. For example, JavaScript might be used to change the style based on the time of day the user is browsing the webpage.
  • Creating in-browser applications, such as games, quizzes, online shopping, file scanning, video, photo editing, word processors, spreadsheets, and more.
  • Randomising elements, such as numbers, images, and more.
  • Displaying the current date and time in the web browser.
  • Changing the colour of a button when a mouse hovers over it.
  • Displaying a countdown on a website that will perform an action when the countdown is finished.
  • Using a dropdown hamburger menu on a website that displays on the top navbar when the screen is below a certain width. Used in responsive web design.
  • Showing or hiding content by clicking a button.
  • Displaying alert boxes that notify users of information and ensure that it comes through to the user.
  • Zooming in and zooming out of a webpage.
  • Displaying webpage animations, such as fading objects.
  • Displaying results similar to what the user has typed in a field. This is commonly used in search boxes that allow users to find what they are looking for on a website.
  • Redirecting a user to another webpage.
  • Generating banner, skyscraper, rectangle, leaderboard, and popup ads, which is what most websites are funded by.
  • Controlling the playback of streaming media.
  • Logging data about the behaviour of visitors, such as the pages viewed and how long they spent on them, and then sending that data to the webmaster. This data can be used for analytics and ad personalisation. Google Analytics is an example of an analytics application.

Security Of JavaScript

Even though JavaScript that comes from an unknown source can be malicious, JavaScript has strong security measures in place that were implemented by Sun Microsystems, such as secure sandboxing. This means that JavaScript is isolated to the browser process and cannot read or write files on a hard drive or inspect the computer hardware. However, it can generate cookie files used to store information. For maximum security, JavaScript can be disabled. But, most websites require JavaScript in order for them to function properly.

If JavaScript was capable of reading and writing files on a hard drive, it would be a huge security risk. Attackers could install malware onto a user's computer without any user interaction.

Cross-site scripting (XSS) is the most common JavaScript vulnerability where an attacker injects malicious JavaScript into a webpage. For example, if an attacker inserted JavaScript into a form field that users fill out when submitting a review and the website performs the action, it is vulnerable to cross-site scripting. This would cause the script to be stored on the server side and run for every visitor that sees that review, even though the JavaScript code cannot be seen on the review. Since JavaScript can be used to do almost anything on a website, it could be used for malicious purposes, such as stealing session cookies from users. Properly encoding, escaping, and sanitising forms will help reduce the risks of XSS attacks.

No, JavaScript is completely unrelated to Java. JavaScript is a web object-oriented programming language and Java is a computer object-oriented programming language. JavaScript is used to develop web-based features that run in a browser only, whilst Java is used to develop applications that do not use web browsers, such as games that are downloaded onto mobile devices.


Feedback

  • Is there anything that you disagree with on this page?
  • Are there any spelling, grammatical, or punctuation errors on this page?
  • Are there any broken links or design errors on this page?

If so, it is important that you tell me as soon as possible on this page.


Comments