JavaScript
Designed by | Brendan Eich |
---|---|
Developer | Netscape |
First appeared | 4 December 1995 |
Website | www |
JavaScript is a high-level programming language that follows the ECMAScript standard. It was originally designed as a scripting language for websites but became widely adopted as a general-purpose programming language, and is currently the most popular programming language in use.[1] JavaScript is usually found running in a web browser as interactive or automated content, ranging from popup messages and live clocks to large web applications. JavaScript is also commonly used in server-side programming through platforms like Node.js,[2] or "embedded" in non-JavaScript applications where the base programming language lacks the high-level functionality that JavaScript offers.
Despite the similarities in name and syntax, JavaScript is not related to the programming language Java. Though the names of both languages are trademarks of Oracle Corporation, the two languages follow different design principles, and are actively developed by unrelated organizations.
Use
[change | change source]JavaScript is typically inserted into HTML when used on the web, either directly in the file in an HTML tag, or linked to a separate file containing the script.
JavaScript, as a full featured scripting language, can be used to provide functionality to a website. Examples include:
- Using AJAX in order to load content without refreshing the website.
- Changing HTML through the Document Object Model
- Changing CSS
- Validating inputs of forms and sending them.
- Tracking users as they move throughout the website.
Frameworks
[change | change source]A majority of websites use what is known as a framework. Frameworks may allow programming to be easier as more predefined procedures are defined within it. Such a library is jQuery.
Video Games
[change | change source]JavaScript can be used to create and run video games in the browser. The modern web has quickly become a viable platform for creating and distributing high-quality games. With modern web technologies and a recent browser, it’s entirely possible to make stunning, top-notch games for the web. JavaScript is blazing fast in modern browsers and getting faster all the time. You can use its power to write the code for your game or look at using technologies like Emscripten or Asm.js to easily port your existing games.[3]
There are also many widely adopted game engines that you can use to develop games with JavaScript and HTML5. Some popular ones include Three.js, Pixi.js, Phaser, Babylon.js, Matter.js, and PlayCanvas. These game engines provide a range of features and tools to help you create sophisticated 2D and 3D graphics without relying on third-party plugins.[4]
Beyond web browsers
[change | change source]JavaScript is also used outside of web browsers. As a scripting language, JavaScript can be used to define the behaviour of applications such as extensions in GNOME Shell.
In addition, there are runtime environments for running JavaScript as a server side programming language. Such an environment is Node.js.
Electron is a framework which allows graphical applications to be made with web technologies, by running on the Chromium browser and Node.js.
Syntax
[change | change source]A JavaScript program is made of a collection of instructions called "statements". A semicolon marks the end of a statement, and allows multiple statements to be placed on the same line. However, it is typical to write each statement on its own line to keep a program file readable.
Variables can be defined in several ways. In an older version named "ES5", variables are defined using the var keyword.[5] In the newer versions after ES5, variables can be defined using const for constant variables and let for local variables.[6][7] The value of constant variables cannot be re-declared or reassigned. Variables assigned using const or let are contained within blocks, while variables assigned using var are contained within functions.
// ES5
var x = 1;
// ES6+
const y = 10; // Cannot be reassigned
let t = 5; // Can be reassigned
Examples
[change | change source]The script below prints "Example" on the screen. The lines that start with //
are comments, which are used to describe the actions of the program.[8]
function sayHi() {
let name = prompt("What's your name?");
// This name is saved to a variable
alert("Hello " + name);
// Whatever the name is, the browser alerts "Hello (name)"
}
sayHi(); // Runs the function sayHi
/* This is also a comment,
but it can span multiple lines.
*/
The code above alerts "Hello " + name
.
If you want to put JavaScript in your HTML, you put it between an opening <script>
and closing </script>
tag. These tags mean that whatever is between them is a script and not text to be put onto the web page. The script below inserts the numbers 1 through 10 at the bottom of a webpage:
<!DOCTYPE html>
<head>
<title>Example page</title>
<script> // This is the script tag
for (let counter = 1; counter <= 10; counter++) {
document.body.innerHTML = document.body.innerHTML + counter + "<br>";
/*
This puts the number, then a new line element (<br>),
at the end of the web page.
*/
}
// End javascript:
</script>
</head>
<body></body>
The for()
loop makes whatever code is between the {
and the }
happen more than one time. In this case, it keeps looping until numOfTimesAround
is equal to 10, then it stops. This means it equals ten. It's a bit confusing at first, but it works.
Differences between Java and Javascript
[change | change source]- In Java, to define a variable, you have to say what type of variable it is: a number, a word, a letter, or more. In JavaScript, this is not necessary.
- In JavaScript, functions are stored as variables (unlike Java). This makes the following code okay in JavaScript:
function sayHi() {
alert("Hi!");
}
sayBye = function() {
alert("Bye!");
}
sayHi();
sayBye();
- JavaScript is interpreted, but Java, in most cases, must be compiled. This means that JavaScript needs to be run by another computer program (an interpreter), but finished Java can be run as its own program.
Related pages
[change | change source]References
[change | change source]- ↑ "Stack Overflow Developer Survey 2021". Stack Overflow. 2021-08-01. Archived from the original on 2022-04-13. Retrieved 2022-04-18.
- ↑ "Server-Side Javascript: Back With a Vengeance". ReadWrite. 2009-12-17. Retrieved 2020-03-11.
- ↑ "Introduction to game development for the Web - Game development | MDN". developer.mozilla.org. 2023-04-14. Retrieved 2023-06-04.
- ↑ Eseme, Solomon (2022-11-21). "Top 6 JavaScript and HTML5 game engines". LogRocket Blog. Retrieved 2023-06-04.
- ↑ "var". MDN Web Docs. Retrieved 2018-03-24.
- ↑ "const". MDN Web Docs. Retrieved 2018-03-24.
- ↑ "let". MDN Web Docs. Retrieved 2018-03-24.
- ↑ "Javascript Tutorial - Comments".
Other websites
[change | change source]- Learn JavaScript Archived 2012-07-16 at the Wayback Machine on the Mozilla Developer Center
- Mozilla's Official Documentation on JavaScript
- https://mazharulislam.xyz/what-[permanent dead link]
- Video - Firefox 2 and Javascript with Mozilla Corp and JavaScript creator Brendan Eich Archived 2007-09-27 at the Wayback Machine
- References for Core JavaScript versions: 1.5
- New in JavaScript: 1.7 Archived 2009-03-08 at the Wayback Machine, 1.6 Archived 2008-06-28 at the Wayback Machine
- List of JavaScript releases: versions 1.0 - 1.7 Archived 2008-05-14 at the Wayback Machine
- Brendan's Roadmap Updates: JavaScript 1, 2, and in between Archived 2006-01-01 at the Wayback Machine - the author's blog entry
- comp.lang.javascript FAQ Official FAQ for the comp.lang.javascript Usenet group
- RFC 4329, a document for the registration of media types related to ECMAScript and JavaScript. The current recommendations are "application/javascript" and "application/ecmascript", although neither is recognized by Internet Explorer.