JavaScript (JS) is a programming or scripting language that adds interactivity to web pages. Examples of this interactivity include interactive maps, animated 2D/3D graphics, text boxes, buttons, etc. It also has uses in non-browser environments such as Node.js, Apache CouchDB, and Adobe Acrobat. JavaScript is NOT equivalent to the Java programming language as both languages have different syntax, semantics, and uses.
JavaScript is typically used inside web browsers by adding JS code to the HTML page. The JS code can run in the browser (client-side) as opposed to running on a web server (server-side).
Three Major Parts of “JavaScript”:
Most text editors can be used to write JS
JavaScript can be embedded directly inside the HTML file or a line of code can be added to the HTML file which will link to an external JS file.
To embed JS a <script>
opening tag and a </script>
closing tag are added with the JavaScript code inserted between the tags.
alert("text")
"text"
document.write("<h1>Hellow World</h1>")
console.log("text")
Command + I
or inspect your web page and navigate to the console.prompt("text", "___")
confirm ("text")
confirm()
function is not a stand-alone input method and instead allows for a Yes/No or True/False question. A pop window will show with text and two buttons. If the user presses OK
the confirm()
function will be true
and if the user presses cancel
or hits the ESC
(escape) key the function will be false
.confirm
is usually used with an if-else
statement in order to provide feedback to the user based on their response.Variables are labels/containers for storing data or data values and can have different names. An easy way to think of variables is in terms of x
, y
, and z
just like algebra. Additionally, variables can be declared in four different ways: using var
, using let
, using const
, and using nothing. A variable can have a declared/defined value or no value (undefined).
For example:
Declaring a variable AND assigning value
let myName = “Armon”
Declaring a variable with NO value:
let backpack;
var
The var
keyword was used in ALL JavaScript code from 1995 to 2015. Keywords let
and const
were added to JS in 2015 and are recommended when declaring variables today. In order for code to run on older browsers, developers must use var
.
const
or let
If a variable is constant and will or should NEVER change then use const
. However, if variables are subject to change then declare them with let
.
All JS variables must be labeled with unique names or identifiers. Identifiers can be short (like a and b) or more descriptive (age, product, usersCollegeMajor). The rules for making unique identifiers are:
Name
and name
are different variables)var
) cannot be used as names'Your text goes here'
42; -5; 0.27
True
False