Follow TV Tropes

Following

Useful Notes / JavaScript

Go To

https://static.tvtropes.org/pmwiki/pub/images/6c8ffe53_a4ce_4f57_b3ff_1908016eae92.png
Advertisement:

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 DOM. 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 server-side scripting (backend) with Node.js, 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.

Advertisement:

History

When browsers were first introduced, they could only display static, non-interactive webpages. Netscape decided to change this by collaborating with Sun Microsystems to add Java applets to their webpages, but soon Netscape decided that the best course of action was to create their own language. JavaScript was first released in the Navigator browser in 1995.


    Code Examples 

Hello JavaScript

console.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 Concatenation

let a = 10; 
let b = 7;
let c = "hi";
console.log(a + b);
// Outputs 17
console.log(b + c);
// Outputs 7hi
console.log("Hello and " + c);
// Outputs Hello and hi

Although it is possible to declare variables without the 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 

For Loops

for(let i=0;i<10;i++){ 
    console.log(i);
}

JavaScript uses a common C-like for 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.

Why did this happen? Think of

    DOM Manipulation 

Note that this only works if JavaScript is in a browser environment!

A Title Change is Needed

document.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 JavaScript cannot find the target element however, it will throw an error.

Changing Fonts

let 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 JavaScript changing properties of all the elements in the page.

Advertisement:

Works that use/are written in JavaScript

    Websites 

  • TV Tropes, in addition to the stuff discussed above, TVTropes uses JavaScript to display notes, make spoiler tags work, and make options like dark-mode and wide load work.
  • The Other Wiki


Top

How well does it match the trope?

Example of:

/

Media sources:

/

Report