What I learned about Java Script so far:
Syntax and what they mean:
var | = | Variable |
const | = | Constant (Like a variable but you cant change it anymore) |
let | = | Like a variable but with block level scoping |
alert(); | = | Opens a dialog box |
document.write(); | = | Writes directly on the page |
console.log(); | = | Writes in console |
prompt(); | = | Opens a box to return a variable |
parseInt (); | = | Converts string to int |
parseFloat (); | = | Converts string to a decima number |
=== | = | Strict equal to |
== | = | Equal to |
> | = | Greater than |
>= | = | Greater than or equal to |
< | = | Less than |
<= | = | Less than or equal to |
!= | = | Not equal to |
!== | = | Strict not equal to |
if ( ) { ;} | = | if true than do |
else ( ) { ;} | = | if not true than do |
Math.random () | = | Generates a random number between 0 to, but not including 1 |
Math.floor () | = | Rounded up |
Math.ceil () | = | Rounded down |
.toUpperCase () | = | Converts every letter to uppercase |
function name () | = | Creat your own function |
return ; | = | Returns a value from a function |
isNaN () | = | Is Not A Number |
throw new Error ('error message'); | = | Creates a unique error |
document.getElementById | = | Selects element by id |
document.getElementsByTagName CAREFUL: Elements(!) not Element |
= | Selects elements by tag (like "p", "li", "h1", etc ) |
document.getElementsByClassName | = | Selects elements by class |
document.querySelector (); | = | Select one element of Id, Class or Tag |
document.querySelectorAll (); | = | Select all elements of Id, Class or Tag |
Element.textContent (); | = | |
Element.innerHTML (); | = | |
Element.attribute (); | = |
Didn't find the proper syntax? Try this!
Running / executing the program
The web browser reads each statemant from the top down to the bottom.
If you want your content displayed in a special order you need to write them exactly like that.
Variables
A variable is like a box.
You can put something inside, look into it and empty it out.
Write a variable like this:
var ( = key word ) score ( = specific name of variable ) = Value;
You can even keep the box empty. ( var score; )
Unusable variable names:
- break
- case
- catch
- class
- const
- continue
- debugger
- default
- delete
- do
- else
- enum
- export
- extend
- finally
- for
- function
- if
- import
- in
- instanceof
- let
- new
- return
- super
- switch
- this
- throw
- try
- typeof
- var
- void
- while
- with
- yield
Those names are reserved for JavaScript. When you use them you'll get a system error.
There might be more in the future.
You can also not use numbers in the beginning of the name.
Values
There are multiple value types:
- Numbers
- Strings
- Booleans
and more.
Concatenation
Combining multiple strings is called concatenation.
You can eather write varName = varName + "add text";
or varName += "add text";
Last one is the shortcut. It takes the varName and adds to whatever is in the "box".
Objects
can be a string, the document, the browser's console, etc
Objects have properties, such as the length of a string.
Objects have methodes, which are actions the object can perform.
Numbers
Integer are whole numbers (like 46).
Floating Point Numbers are numbers with a dezimal point
(like 45.813).
Scientific Notations are used to represent realy small or realy large numbers (like 9e+5 instead of 900 000).
Functions
Functions are used to define a way of doing.
You first tell the program how it has to do the task. Then you can just tell it to execute it.
With this you can run multiple lines of code with only one command as often as you like.
Example:
function name (i){
console.log(i);
}
Excess the functin with name(parameter);
You even can call a function within a function.
Scope
Global Scope: When you're starting to write in your document, you are in the global scope. There is only one global scope throughout a JavaScript document. A variable is in the Global scope if it's defined outside of a function. Variables inside the Global scope can be accessed and altered in any other scope.
Local Scope: Variables defined inside a function are in the local scope. And they have a different scope for every call of that function. This means that variables having the same name can be used in different functions. This is because those variables are bound to their respective functions, each having different scopes, and are not accessible in other functions.
Loops
Loops are used to do a task multiple times without repeating your code.
Repeating code snippets would make your code iternaly long and sometimes you don't even know how often the task is supposed to run because it depends on the user's behavior.
There are multiple options for loops.
Never run a loop without a stopping condition. This can end in a brackdown of the browser.
for-Loop
for (var i = 0; i < object.length; i++) {
··code··
return ...;
}
forEach-Loop
var array1 = [‘ a’, ‘ b’, ‘ c’];
array1.forEach(function(element) {
··code··
});
for...of-Loop
for (variable of object) {
··code··
}
for...in-Loop
for (variable in object) {
··code··
}
while-Loop
var n = 0;
var x = 0;
while (n < 3) {
n++;
x += n;
}
do...while-Loop
var i = 0;
do {
i += 1;
··code··
} while (i < 5);