Introduction to Javascript

What JavaScript is:

NOT

JAVA

(Blame marketing d*cks!)

Java

import from java.io.File;
import from com.lowagie.text.Document;

public static void fileCopy( File in, File out )
            throws IOException
    {
        FileChannel inChannel = new FileInputStream( in ).getChannel();
        FileChannel outChannel = new FileOutputStream( out ).getChannel();
        try
        {
            // magic number for Windows, 64Mb - 32Kb)
            int maxCount = (64 * 1024 * 1024) - (32 * 1024);
            long size = inChannel.size();
            long position = 0;
            while ( position < size )
            {
               position += inChannel.transferTo( position, maxCount, outChannel );
            }
        }
        finally
        {
            if ( inChannel != null )
            {
               inChannel.close();
            }
            if ( outChannel != null )
            {
                outChannel.close();
            }
        }
    }

Look out for:

  1. imports: from java. and com.
  2. Keywords: public, private, protected, etc.
  3. Variables are cast with a type: int age = 20, string name = "James"
  4. No function keyword
  5. == not ===
  6. Lots of new[ing up]

What is Javascript

Javascript is:

  • a convenience language
    • it sits on top of C++ (PC) or Objective-C (Mac)
  • it is an 'interpreted' language
    • It is NOT a language on it's own. It will not compile directly to create byte code that a computer can run
    • It therefore needs:
      • an 'interpreter' program to transform and run it, and
      • an environment to run IN.

What did all that mean?

  1. You need a computer program to run JavaScript.
  2. The most common one you will find is a browser
    1. Here JavaScript is used to manipulate a page and/or keep track of 'state' (so, remember numbers for you, etc.)
  3. JavaScript can exist outside of the browser too, though (See Node.js)
  4. In fact, we can run javascript anywhere that there is the resources and a program to run it. Think IOT (Internet Of Things)

C language

C program (like browser) talks to OS (also in C, so direct conversation) => "Reserve me some space on the RAM chips (or HD) for this value"

JavaScript

Javascript is interpreted into C code and then that code is run on the C layer => 

C layer reserves memory on javascript's behalf.

(Javascript has no direct access)

Every feature in JavaScript is the result of someone writing some C code and labelling it with a name that is then used when you type it in the JavaScript

Interfaces

In short, in every case, <some ACTUAL programming language> sets up a way to read written javascript text and turn it into the code OF THAT LANGUAGE. So for javascript to do anything it requires runtime AND an interface.

 

Browsers: v8 engine & DOM interface

Servers: Node.js (both)

IOT:<some runtime> & <some interface>

 

So, some programming to run it and some to let it control things.

The Web: JavaScript's Natural Environment

Brief History

 

So JavaScript was born to control the interface between Browsers and Servers

Back-End vs. Front-End

  • Back-End (Server): Programming for the machine that gives you the document. May include:
    • Sending you back a document
    • Getting, processing & returning data from a database
    • Calling someone else's servers to get information
    • Keeping 'state' for your application (think: logged in/out)
  • Front-End (Client/Browser): Programming for what happens to the document when it is with the user and the user interacts with it. May include:
    • Document manipulation (show/hide)
    • Responding to clicks, hovers, etc.
    • Calling server for more information
    • Manage local resources (e.g. localStorage, etc.)
    • Local programming (Maths operations, etc.)

About JavaScript

  • History
  • Scripting language 
    • developed to coordinate things happening at uncertain intervals
  • THE lingua franca (bridging language) of the internet
  • Developed by Brendon Eich in 10 days in May 1995 for Netscape
    • Amazingly Expressive
    • Has some TERRIBLE bits to it
    • Backwards Compat - can never undo a mistake
    • The war of styles (Class programmers vs. JS Coders)
      • No wrong answers - multiple ways to code
  • ECMAScript - an official standard
  • ES6,7 (ES2015, ES2016) Harmony - it's all just changed

Yes, it's quite tough going

Running JS in the Browser

DEV TOOLS:

  1. right click then 'inspect element'
  2. Go to console tab
  3. type in box and press return

 

Good demo, but this (like the Node command line) is only an emulation! It is not bullet-proof!

INSTALLFEST COMMENCE!!

Installfest

  • VS Code (https://code.visualstudio.com/download)
  • Homebrew (if mac)
  • Node
    • ​brew install node (mac)
    • https://nodejs.org/en/download/ (windows)
    • You should be able to open a terminal and type 'node' and hit enter, and get a prompt.
    • ONCE YOU HAVE npm installed, install everything else, like trash, typescript, babel, jest, etc. from there.
      • Only use brew to install standalone software like databases, python, etc.
  • ​My terminal is OhMyZSH: Tutorial Here