Javascript

Everything About Fiction You Never Wanted to Know.
Revision as of 20:55, 21 November 2022 by Ilikecomputers (talk | contribs) (more information + CSS pothole to correct page not disambiguation + basic instructions on writing javascript + why javascript is relevant to tropes)


  • Main
  • Laconic
  • Wikipedia
  • All Subpages
  • Create New
    /wiki/Javascriptwork


    First off, Javascript is NOT Java.

    However, it was called Javascript to piggyback off of the similar name, but the two languages are quite different. They share some mild syntax similarities, but are fundamentally two different languages, with different designs and goals.

    In short, Javascript, alongside HTML and CSS, make up the basic building blocks of almost all webpages. Where HTML serves as the basic layout and CSS handles styling, Javascript is used for all the fancy stuff like popup windows, executing program code in a webpage, and adding interactive features like polls. Javascript is mostly a client side language, meaning that it runs in the web browser instead of the server the website is hosted on, although software such as node.js allows it to be used on servers. Furthermore, tools such as Electron and React Native allows developers to construct desktop apps and mobile apps (respectively) using web tools, handling logic and processing with Javascript.

    Javascript features automatic semicolon completion. Whereas in other languages you have to manually end your line with ;, Javascript can detect where semicolons should be in your code, and execute the code as if they were there. However, using this feature is frowned upon by some developers, as it breaks standards set out by other languages without the feature, and the engine may occasionally get the completion wrong.

    To write your first Javascript program, press F12 on your keyboard, or Ctrl + Shift + I. This should bring up developer tools. Click on the "console" tab, and type in (case sensitive!) console.log("Hello World!");. Be careful when copying and pasting code into there, however, as some malicious actors can ask you to run code which does negative effects such as stealing your password or post spam. Press enter to run the code. The program that you just wrote is called "Hello World", and is among the first any new programmer writes. It simply logs the words Hello World into the console. Instead of "Hello World", you can choose any other phrase, like "abcde", or "hi Javascript overlords", like with console.log("abcde"). You can also choose to log the result of any arithmetic operation, like console.log(1 + 2). It's also possible to log the result by just typing 1 + 2 (the console will log the result returned by evaluating any one line expression), however the result will not be logged if it is executed in a separate .js file.

    To get user input, use the prompt() function, which only works if your code is running in a browser (getting user input on server-side Javascript is a lot harder). Assign that result to a variable so it can be used later with let variableName = prompt(). Note the use of CamelCase. The result will always be a string, so prompting for the user for two numbers and adding them together can produce unexpected results. Javascript has different data types, string and number (float) being two of them. String and number are represented differently, thus their operations and what you can do with them differs. To remedy this, use the function parseInt() to extract a number from the string input. Thus, one method to write a program which adds two user input numbers can be:

    let numberOne = prompt("Give me a number");
    let numberTwo = prompt("Give me another number");
    let additionResult = parseInt(numberOne) + parseInt(numberTwo);
    console.log(additionResult);

    And that's just scratching the surface. We haven't even mentioned functions, arrays, objects, iteration, or selection. Javascript is a powerful language that powers most of the web, but can be picked up pretty quickly by beginners. If you are interested in learning further, there are plenty of resources on the internet to help, like the (free) course over at freecodecamp.org.

    So how does all this relate to tropes, the very thing you're on the site for? Well, Javascript as a language (along with PHP) powers this very website! Some buttons (or page elements) have events, which runs Javascript code when you click them (or does any other action to trigger that event). If you open developer tools again (F12), and go the inspector tab this time, any element with an event tag runs some sort of Javascript, and anything in between <script> and </script> tags contains script as well.