| HTML | CSS | JavaScript |
|---|---|---|
| Structure | Design | Behavior |
| Client-side, browser organizes markup | Client-side, browser visualizes design | Client-side, browser interprets commands |
| Hyper Text Markup Language describes the various components of a web document. | Cascading Style Sheets stylize the elements in a web document. | JavaScript adds interactivity and behaviors to a web document. |
| Compiled | Interpreted |
|---|---|
| C++, Java | JavaScript, PHP |
| Compiled as Machine Language | Code saved as you wrote it |
| .exe - executable file Self-standing program |
.js, .php requires library to interpret commands |
| Fast, speed of performance | Will run in a browser |
| Programming Language | Scripting Language |
client-side JavaScript adds behavior to otherwise static web content. Anyone with a web browser and a simple text editor has a complete development environment.
client-side JavaScript combines the scripting ability of a JavaScript interpreter with the Document Object Model (DOM) defined by a web browser. JavaScript considers each of the items in the document tree to be objects. Use JavaScript to manipulate those objects or nodes.
JavaScript programs are written using the Unicode character set. UTF-8 (8-bit Unicode Transformation Format) is backward-compatible with ASCII (7-bit). The 16-bit Unicode encoding can represent virtually every written language in common use on the planet. (This is an important feature for internationalization and is particularly important for programmers who do not speak English.)
JavaScript is a case-sensitive language. This means that language keywords, variables, function names, and any other identifiers must always be typed with a consistent capitalization of letters.
JavaScript ignores spaces, tabs, and new lines, so use white space for clarity.
Simple statements in JavaScript are generally followed by semicolons (;), just as they are in C, C++, and Java. The semicolon serves to separate statements from each other.
Comments may be written using
// single line or
/* open comments and
then close comment for multiline */
An identifier is a name. In JavaScript, identifiers are used to name variables and functions, and to provide labels for certain loops in JavaScript code. The naming rules are: the first character must be a letter, an underscore (_), or a dollar sign ($). Do not use spaces. Do not use any special characters other than _ or $. Use the key terms "var" (variable) or "const" (constant) or "let" (post 2015 with ES6) to identify variables. "const" and "let" were introduced in 2015 with ES6, so they do not function in old browsers. "const" used for variables that will not be reassigned. "let" is limited to the block/function (bounded by {}) in which it is declared. This is a brief article explaining the differences Var, Let, and Const - What's the Difference?
var i
let my_variable_name
const const PI = 3.14159265359; //const must be assigned a value upon declaration
var _dummy
var $str
Computer programs work by manipulating values, such as the number 3.14 or the text "Hello World." The types of values that can be represented and manipulated in a programming language are known as datatypes. JavaScript allows you to work with four primitive datatypes:
Besides numbers, strings, boolean and symbols, JavaScript also defines two trivial datatypes each with a single value:
In addition to primitive datatypes, JavaScript supports a composite datatype known as an object. An object represents a collection of values.
var person = {firstName:"John", lastName:"Doe", age:50};
"Consider a person as an object. Properties are the values associated with the object. The persons' properties include name, height, weight, age, skin tone, eye color, etc. All persons have these properties, but the values vary. Objects also have methods or functions. Methods are the actions that can be performed on objects. The persons' methods could be eat(), sleep(), work(), play(), etc." - w3schools - JS Objects
An array is an object that stores multiple values in a single variable.
An array may contain a string or numeric data.
var toys = ['ball', 'bat', 'puzzle', 'phone']
A function is an object with executable code, functions may be invoked to perform some kind of operation.
<script>
var a = 5;
var b = 6;
function product(a, b) {
return a*b;
}
</script>
(A method is a fuction that is a property of an object.)
String variables
var greeting = "Hello, how are you?"
document.write(greeting);
Numeric Variables
var score = 0;
Array
May contain a string or numeric data.
var room = ['bed', 'dresser', 'mirror', 'lamp']
Arithmetic operators are used to perform arithmetic between variables and/or values.
Example Expression: x = y + 2
Arithmetic Operators: +, -, *, /, %, ++, --Assignment operators are used to assign values to JavaScript variables.
Assignment Operators: =, +=, -=, *=, /=, %=
The operator += adds the value on the right side to the variable on the left.
If variable count = 6, count += 1 sets count to 7.
count += 1 is the same as count = count + 1
View Table
Comparison operators are used in logical statements (such as an if statement) to determine equality or difference between variables or values.
Comparison Operators: ==, ===, !=, >, <, >=, <=
View Table
Logical operators are used to determine the logic between variables or values.
Logical Operators: &&, ||, ! (and, or, not)
View Table
++x
--y
x += 22
y -= 3
document.write("You have " + messages + "messages.")
If the variable messages is set to three, the document presents:
You have 3 messages.
Escape characters (such as \ ) may be used to insert quotations marks in strings as well as various special characters such as tabs, new lines, carriage returns.
View Table
Global variables are defined outside of any functions (or within functions but without the var keyword). Every part of a script can have access to a global variable.
var a = 345; //Global scope
function test() {
var b = 678; //Local scope
if (a == 345) var c = 910; //Local scope
}
Objects - HTML elements that the browser interprets.
Properties - the values of HTML elements.
Methods - things that objects can do: buttons click(), windows open(), text may be selected(). Parentheses signals reference to a method. List of JavaScript Built-In Methods
dot syntax - reference an object or describe a process by putting together objects, properties and methods separated by a period (getElementById, querySelector):
document.getElementById("greeting").innerHTML =
"Hello" + name + "!";
OR
document.querySelector("#greeting").innerHTML
Events are actions that the user performs when visting a page, such as submitting a form or mouse over an image. JavaScript deals with events using commands called event attributes. An action by the user triggers an event attribute in your script.
By breaking up a page content into semantic sections using using either HTML5 semantic tags or divs for block level elements and spans for inline elements, you are better able to target scripts to particular sections of a page.
Semantic tags, classes and ids are attributes used by both CSS and JavaScript. CSS to apply design rules and JavaScript to target specific elements and their behavior.
getElementById()
getElementsByClassName()
getElementsByName()
getElementsByTagName()
querySelector()
querySelectorAll()
document.write() document.write() is the method used to dynamically output HTML text into an HTML document while it is being loaded into the browser.
"Method" is the object oriented term for function or procedure. A method is a function that blongs to an object.
<script> and </script> Script tag is used to embed JavaScript code in an HTML file. It may be placed in the head tags or body tags. Place it in the head tags if you wish to execute a script when a page loads.
You may also include files of JavaScript code. Do so within the head tags:
<script src="script.js"></script>
<script src="http://www.othersite.script.js"></script>
<script src="script.js" defer> </script>
The script tag has the defer attribute to not run a script until after page loads. This way you may nest the script tag in the head tags but not slow the rendering of the HTML.
<noscript>
<style>
body {display:none;}
</style>
<div class="noscriptmsg">
The web is happiest with JavaScript enabled.
</div>
</noscript>
The CSS inside the noscript tag will hide all of your page content, and instead display a "no JS" message that you write. (This IS NOT RECOMMENDED.)