You can build:
Scripts for webpages
It's also excellent at IO Intensive Jobs, made possible by a Non-Blocking Event Loop, that can queue up work in the background without clogging up the worker pool.
Use that to your advantage to make the code more readable!
You insert a comment line with:
You probably won't need to place a lot of comments if you think carefully about how you name your functions and variables.
But with some complex functions, it can make your code more readable and easier to understand.
let to declare a variable. You use
let when you need to change the value of the variable, otherwise you always use
If you don't declare a variable, it automatically becomes Global Scope, even if the first time you use it is in a function!
You don't want to use Global Scope often. (Only when necessary)
Use Local Scope variables as much as possible.
Always declare the global variables at the top of the file, and the local variables at the top of the Function.
Defining = assigning a value to a variable.
It's also not required to assign a value to the variable when you declare it. But you can if you want to.
A variable or property becomes undefined when:
It hasn't been assigned a value.
When it doesn't exist in an array.
When the property doesn't exist.
When the variable or property got deleted.
I check if something is undefined quite frequently in order to see if it has been assigned a value or if it exists.
These operators are used often:
== Will check on value (but it does a type conversion first, so it only checks value, not type!)
=== Will check for equal value and type, if they are both the same it will return True. Most of the time this will be better to use than
< Less than
> Greater than
<= Less than or equal to
>= More than or equal to
!= Not equal to
!== Strict not equal to
An object doesn't exist
A string is empty
A function returns 0 (You can use this to return if a condition within the function is not met)
An object exists
A string contains a value other than empty
A function returns 1 (You can use this to return if a condition within the function is met)
Often used with Comparison Operators to check if a condition is met.
If you use a return with your functions, you can leave out the 'else', because when it returns it leaves the function.
I like the while loop more for looping over a boolean value, do something while x == false for example.
You then break out of that loop by assigning 'true' to x.
It's also possible to create an infinite loop with:
The for loop works better if you want to iterate over data X amount of times.
You can declare the index and increase the index every loop, within the declaration of the for loop. Like this:
In the code example above, number is a parameter, it's something you can pass to the function to use it within that function / scope. It can be for example an object, a class, a function, or a variable.
If you give more arguments than the function has parameters, it will just ignore them and the function would still work.
If a argument is missing, it will give some trouble.
A function expression is a function, but you use it a bit differently. Instead of giving the function a name, it's assigned to a variable.
It essentially does the same as using a function declaration (the function fly example), but there are some key differences:
Function expressions are better for Objects
The expression results are assigned to the variable afterwards.
I think the function expression is cleaner when you create a function that calculates a value.
That way you can use
calculate() when you want to assign a value, and then later use
calculate as a variable.
This can also be done with a function expression, you get a reference to that function.
With a function expression, the code executes at runtime.
With a function declaration, it gets evaluated before the rest of the code.
The difference is subtle, and ultimately it's a matter of style.
A function is basically also a variable, with the difference being that you can invoke them.
If you have only 1 parameter, you could also remove the parenthesis. Or if you don't have any parameters just use empty parenthesis:
An array is basically a list.
Examples of data that go well with arrays are:
Shopping cart items
Saved input data
Or even an entire product catalog (but you would probably want to use a Database for that).
Each entry of the array has an index. (it starts with 0, so if you want the 5th item, the index = 4)
You want to limit the amount of items in an array to max hundreds or thousands, because each item will take up some memory.
Objects are basically a collection of multiple properties, you can assign as many or as few as you like.
You can change them using, for example:
Note: when you call the object, you're actually referencing a pointer to the object,
If you pass an object to a function, you're making a copy of the reference, thus still changing the object if you change it within the function.
You can even add behavior to objects by calling a function from a property:
When a function is called from an object, we call them 'Methods'.
If you want to use a property from the object, in a method, you need to add 'this.'
You can access an object multiple different ways:
This refers to the current Object it's in, so that can be a property or function within the scope of the object.