JavaScript (often abbreviated as JS) is a dynamically typed, prototyped-based, object-oriented Programming Language. JavaScript is one of the core web technologies, along with HTML and CSS. It is used in almost every website as a way to provide interactive content, communicate with backend services, and arbitrarily manipulate the DOMnote Document Object Model, a programming interface for web documents. Even this very wiki uses JavaScript for features such as the ability to open and close folders. More recently, JavaScript has become a popular choice for both client-side (frontend) and server-side scripting (backend) with React and Node.js respectively, and for writing non-browser applications.
You can try out JavaScript in your browser by going to View > Developer > JavaScript Console if you're on desktop or go to a mobile friendly editor.
HistoryWhen browsers were first introduced, they could only display static, non-interactive webpages. Netscape decided to change this by collaborating with Sun Microsystems to add
Javaapplets to their webpages, but soon Netscape decided that the best course of action was to create their own language.
JavaScriptwas first released in the Navigator browser in 1995.
Code Examples
Hello JavaScriptconsole.log("Hello World!");
Outputs Hello World to console or command line depending on the environment. Single quotes can also be used for strings.
Variable Declaration and Concatenationlet a = 10;
let b = 7;
let c = "Bob";
console.log(a + b);
// Outputs 17
console.log(b + c);
// Outputs 7Bob
console.log("Hello, " + c);
// Outputs Hello, Bob
Although it is possible to declare variables without the 'let' keyword, it's not recommended because declaring variables in that way always makes it a Global variable regardless of context and that can lead to errors.
note Sometimes, older programs will use 'var' instead of 'let' but most recommend let now. See here why you should use let. For Loopsfor(let i=0;i<10;i++){
console.log(i);
}
uses a common
C-likefor loop. This style is used in most languages.
The Event Loop
When you write a program like this:
let n = 0;
while(true){
console.log("I have run "+n+" times!");
}
You might expect that the program will output an infinite amount of numbers but what you might not expect is that this program will at best, force you to close a tab, and at worst,
Force You To Restart Your Entire Computer.DOM Manipulation
Note that this only works if JavaScript is in a browser environment!
A Title Change is Neededdocument.getElementsByTagName('h1')[0].innerText = "I'm In!"
// Changes the text of the first h1 tag to I'm In!
This program, if run on this webpage, will change the title of this article to I'm In!. If
JavaScriptcannot find the target element however, it will throw an error.
Changing Fontslet all = document.getElementsByTagName("*");
let n = all.length;
for (let k = 0; k < n; k++) {
all[k].style.fontFamily = '"Comic Sans MS", "Chalkboard"';
}
This is an example of
JavaScriptchanging properties of all the elements in the page.
Works that use/are written in JavaScriptWebsites
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4