Please enable JavaScript.
Coggle requires JavaScript to display documents.
javascript (What Can JavaScript do? (Validate data, Reacts to events…
javascript
What Can JavaScript do?
-
Reacts to events (mouse events, keyboard events, etc.)
-
-
-
Comparisons
-
Not equals. In maths the notation is ≠,
in JavaScript it’s : a != b.
Greater/less than or equals: a >= b, a <= b.
-
Greater/less than: a > b, a < b.
DEVELOPER CONSOLE
-
On the right, there is a clickable link to the source.
Press F12 or, if you’re on Mac, then Cmd+Opt+J.
Debugging in Chrome
The Resources zone lists HTML, JavaScript, CSS and other files, including images that are attached to the page. Chrome extensions may appear here too.
-
The Information and control zone is for debugging, we’ll explore it soon.
-
The toggler button opens the tab with files.
Let’s click it and select index.html and then hello.js in the tree view.
-
ARRAY METHODS
-
push(...items) – adds items to the end,
pop() – extracts an item from the end,
shift() – extracts an item from the beginning,
-
splice(pos, deleteCount, ...items) – at index pos delete deleteCount elements and insert items.
slice(start, end) – creates a new array, copies elements from position start till end (not inclusive) into it.
concat(...items) – returns a new array: copies all members of the current one and adds items to it. If any of items is an array, then its elements are taken.
-
indexOf/lastIndexOf(item, pos) – look for item starting from position pos, return the index or -1 if not found.
includes(value) – returns true if the array has value, otherwise false.
find/filter(func) – filter elements through the function, return first/all values that make it return true.
findIndex is like find, but returns the index instead of a value.
-
-
sort(func) – sorts the array in-place, then returns it.
reverse() – reverses the array in-place, then returns it.
-
reduce(func, initial) – calculate a single value over the array by calling func for each element and passing an intermediate result between the calls.
To iterate over elements:
forEach(func) – calls func for every element, does not return anything.
-
comments
Multiline comments start with a forward slash and an asterisk / and end with an asterisk and a forward slash /
-
-
Overall architecture, high-level view.
-
Important solutions, especially when not immediately obvious.
-
-
Put them only if it’s impossible to make the code so simple and self-descriptive that it doesn’t require those.
Increment/decrement
- Decrement -- decreases a variable
When the operator goes after the variable, it is called a “postfix form”: counter++.
-
-
-
-
-
Assignment
-
An operator always returns a value. That’s obvious for most of them like an addition + or a multiplication *.
But the assignment operator follows that rule too.
The call x = value writes the value into x and then returns it.
The “else” clause
The if statement may contain an optional “else” block. It executes when the condition is wrong.
Several conditions: “else if”
Sometimes we’d like to test several variants of a condition. There is an else if clause for that.
The “if” statement
The if statement gets a condition, evaluates it and, if the result is true, executes the code.
string
A String is a collection of characters (letters, numbers, punctuation, ….)
-
alert
-
• In JS, an alert is a pop-up window that displays information
CONVERSIONS:
ToString – Occurs when we output something, can be performed with String(value).
ToNumber – Occurs in math operations, can be performed with Number(value).
OPERAND
-
An operator is UNARY IF it has a single operand. For example, the unary negation - reverses the sign of the number
-
"use strict"
-
- The strict mode is supported by all modern browsers.
there are several language features like “classes” and “modules” that enable strict mode automatically.
- The "use strict" directive switches the engine to the “modern” mode, changing the behavior of some built-in features. We’ll see the details as we study.
Variables
- The name must contain only letters, digits, symbols $ and _.
- The first character must not be a digit.
To create a variable in JavaScript, we need to use the let keyword.
words let, class, return, function are reserved.
We can use variables to store goodies, visitors and other data.
-
-
var – is an old-school variable declaration. Normally we don’t use it at all,
const – is like let, but the value of the variable can’t be changed.
A number
-
NaN represents a computational error. It is a result of an incorrect or an undefined mathematical operation, for instance
Infinity represents the mathematical Infinity ∞. It is a special value that’s greater than any number.
-
prompt()
• Very similar to alert, but wants input.
prompt(“Enter your name: ”);
-
-
Coding style
-
Code first, then functions
For a really short code, one line is acceptable: like if (cond) return null.
But a separate line for each statement in brackets is usually better.
-
-
-
If an expression has more than one operator, the execution order is defined by their precedence, or, in other words, there’s an implicit priority order among the operators.