What is Javascript?

Javascript is a fronend programming language that drives the web and internet. Every interaction on a website could be traced back to javascript and its functionalities.

Resources:
W3Schools
feecodecamp.org
javascript30.com

Console.log output

Console.log is similar to the print() function in python. However, running this command does not alter the content of the webpgae in anyway. The output of this command actually outputs to a terminal "Console" on the webpage. This could be useful for debugging or reviewing our code.

console.log("Hello World!")
Hello World!

Variables

Variables exist in Javascript much similar to variables in python. We could define a variable in our javascript with the var keyword.

var msg = "Hello World!";
console.log(msg);
Hello World!

Functions

Functions could be defined in Javascript with the function keyword, a function name, and arguments to the function. Moreover, the code within the function would need to be wrapped in curly braces (squigglies).

function logIt(ipt) {
    console.log(ipt)
}

logIt("Hello World");
logIt("Foo Bar!")
logIt(1337)
Hello World
Foo Bar!
1337

Dynamically and loosely typed language (string, number)

Javascript is a loosely typed language which means that the type of information to be stored does not need to be specified in advance.

function logItType(output) {
    console.log(typeof output, ":", output);
}
console.log("Looking at dynamic nature of types in JavaScript")
logItType("hello"); // String
logItType(2020);    // Number
logItType([1, 2, 3]);  // Object is generic for this Array, which similar to Python List
Looking at dynamic nature of types in JavaScript
string : hello
number : 2020
object : [ 1, 2, 3 ]

Building a function class

The this keyword refers to the function itself, similar to self in python. The variables defined in this way are isolated within their own scope.

Moreover, the JSON string created in the javascript file could be used as a message to communicate between frontend and backend. The JSON is a way to serialize data to transfer it over a network. Meaning we could use JSON to communicate between javascript programs and python programs.

// define a function to hold data for a Person
function Person(name, ghID, classOf) {
    this.name = name;
    this.ghID = ghID;
    this.classOf = classOf;
    this.role = "";
}

// define a setter for role in Person data
Person.prototype.setRole = function(role) {
    this.role = role;
}

// define a JSON conversion "method" associated with Person
Person.prototype.toJSON = function() {
    const obj = {name: this.name, ghID: this.ghID, classOf: this.classOf, role: this.role};
    const json = JSON.stringify(obj);
    return json;
}

// make a new Person and assign to variable teacher
var teacher = new Person("Mr M", "jm1021", 1977);  // object type is easy to work with in JavaScript
logItType(teacher);  // before role
logItType(teacher.toJSON());  // ok to do this even though role is not yet defined

// output of Object and JSON/string associated with Teacher
teacher.setRole("Teacher");   // set the role
logItType(teacher); 
logItType(teacher.toJSON());
object : Person { name: 'Mr M', ghID: 'jm1021', classOf: 1977, role: '' }
string : {"name":"Mr M","ghID":"jm1021","classOf":1977,"role":""}
object : Person { name: 'Mr M', ghID: 'jm1021', classOf: 1977, role: 'Teacher' }
string : {"name":"Mr M","ghID":"jm1021","classOf":1977,"role":"Teacher"}