Javascript

Everything About Fiction You Never Wanted to Know.
(Redirected from JavaScript)


  • Main
  • Laconic
  • Wikipedia
  • All Subpages
  • Create New
    /wiki/Javascriptwork
    What's "1" + "1"? Did you guess "11"?[1]

    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 allow developers to construct desktop apps and mobile apps (respectively) using web tools, handling logic and processing with Javascript.

    Most other programming languages require you to put semicolons at the end of a line to denote that a line of code has ended. Javascript does this automatically; it 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, therefore manually inserting semicolons at the end of each line is recommended.

    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. Think of a variable like a box for storing information: you can give it a value, read the value, or change the value. The result from prompt() will always be a string -- designed to store letters and sentences, not numbers -- 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. You know how you put "Hello World" in quotes in the previous program? Those quotes tell Javascript that it should be a string, not the name of a variable. Strings 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);

    In almost every single programming language not descended from BASIC, including Javascript, the operation == is used to compare two numbers. Instead of saying 3 = 3, you instead say 3 == 3. This is because = is already used to assign numbers to a variable. For example, number = 3 tells that the value three should be assigned to the variable number. number == 3 checks if the thing contained within the number variable is equal to three. With this in mind, an if statement looks like this:

    if (number == 3) {
    console.log("This number is equal to three!");
    }

    That's enough for now; our main focus is on tropes, not code. We haven't even mentioned functions, arrays, objects, iteration, build in methods, weak typing, or any of the advanced stuff like promises and the event loop. 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.

    1. Then what's "1" - "1"? It's 0.