Not be confused with Java; although both are trademarks of Oracle, they are completely different languages with different syntax and uses.
JavaScript is commonly, but not exclusively, known as the scripting language of websites. It is a lightweight, interpreted language with dynamic capabilities.
Examples of non-web based uses for JavaScript include; Node.js, jQuery, Adobe and others.
ECMA-262 and ECMA-402 are the specification standards for JavaScript.
JavaScript is traditionally included in an HTML page by the author/developer and executed when the user receives and loads the page information. As opposed to executing code before transmission, the code is executed after receiving. Nowadays, there are instances of either before or after use case scenarios as to when JS would be executed.
JS is distinguished by 3 major parts:
JS can be embedded directly within the HTML file (<script> is the opening and </script> is the closing) or linked externally from a JS file.
The alert() function will print whatever is within parentheses in a pop-up box with an “OK” button; rarely used.
alert("Welcome to Class :)");
document.write() can be used to write text or inject other snippets of code through parts of the HTML file, for example recalling variables and their data
document.write("You are currently enrolled in the ITTP through GICW.");
will simply output “You are currently enrolled in the ITTP through GICW.” wherever the snippet of JS code is inserted in the doc.
console.log()
prints debugging info on the JS console that shows warnings and errors generated by JS execution.
let name = prompt("What class is this?:", "102? 201? 301?");
document.write("You are currently enrolled in ", name, " through GICW ITTP");
will show a pop-up with text provided, asking “What class is this? and textbox for users to fill in the second pair of "" with some pre-filled text. The value input is returned upon pressing ‘OK’. document.write is updated with said value; pre-filling the second value, with an output of “You are currently enrolled in ___ through GICW ITTP.” Canceling or pressing escape will result in ‘null’ return.
confirm() function will ask the user to answer a question in a Yes or No fashion in a pop-up window. Basically, if yes, do this; else no, do this, type function. if equals yes, else equals no/alternative.
if (confirm("Shall I print Welcome to class?")) {
document.write("Welcome to class!");
} else {
document.write("No class today... ):");
}
Pressing ‘OK’ at the prompt will print “Welcome to class!”, ‘Cancel’ will print “No class today… ):”.
Variables are containers for data. Much like basic algebra; giving letters or phrases a shorthand / other value. The modern ways to declare are let and const; var and automatically declaring (when undeclared variables are first declared, x = so and so) are outdated methods to declare.
The = sign is referred to as the assignment operator used in declarations of variables (equal to is written as ==). Variables can be used to do arithmetic, just as they can be used to combine strings together.
let x = 5;
let y = 6;
let z = x + y;
Outputs 11 for the value of x wherever it may be used or called for in your file.
Similarily for const; which should be used when the value declared in the variable should not be changed for any reason.
Variables can be identified with identifiers; like letters or be descriptive, such as names or phrases.
One statement/declaration can hold many variables within it and span multiple lines, just separate them with commas (,).
let blue = 58, yellow = 72, place = "My house";