Jscheatsheet
Further source: https://developer.mozilla.org/id/docs/web/javascript/a_re-introduction_to_javascript
Data type
Number: (15 digits)
- Figures without decimal: 10, 123456, 3000
- Decimal numbers: 3.14, 0.5, 100.00
- Exponents: 123E5 // 12300000, 123E-5 // 0.00123
- Negative numbers: -0.134, -2, -100.00
- Octal number (base 8)
- Hexadecimal numbers (base 16)
- Special numbers: -infinity, nan
Operator
- Unary (requires 1 operand) a. typeof number, string, boolean
- Binary (requires 2 operand) a. Arithmetic: +, -, _, /, % (modulus) Precedence Operator: Kurung, Kali, Divide, Add, Less. b. Assignment: =, +=, -=, _ =, /=, %= c. Comparison (comparison): ==,! =, ===,! ==,>,> =, <, <= D. Logic: &&, ||,! e. String: Cumatination +
- Ternary (requires 3 operand) a. conditional (condition)? right: wrong (x % 2 == 0)? "Even": "Odd"
String Escape Character
- 0 = NULL
- '='
- "="
- =
- n = new line
- t = tab
- B Backspace
- uxxxx = unicode
Variable
Declaration: Register variables into the appropriate scope.
Initialization: Providing memory for variables.
Assignment: Determine specific values into variables.
Variable Requirements:
- without spaces.
- Camelcase.
- not starting with numbers.
- Do not use keywords & reserved word.
- Variable Scope: Local & Global.
Var, Let & Const
Refactoring: a process of changing the code to be better without changing its functionality.
Alasan:
- Readability
- DRY, dont repeat yourself
- Testability
- Performance
- Maintainability
Control flow
Repetition (loop)
while a. while (condition) {action}; b. the initial value of while (termination condition) {action; Increment/Decrement; }
for (initial value; termination conditions; increment/ decrement) {action; }
For of (String, Array, Arguments, Typedarray, Map, Set, User-Defined iterables) Const Name = 'Rama'; for (const n of name) {console.log (n); }
for in (enumerable / property from object) const mhs = {name: 'rama', age: 29, email: '[email protected]',}; for (m in mhs) {console.log (mhs [m]); }
do while
Selection (selection)
- if else if (condition) {action 1; } else {action 2; }
- else if if (condition 1) {action 1; } else if (condition 2) {action 2; } else if (condition n) {action n; } else {default action; }
- switch switch (expression) {case "value 1": action 1; [Break;] Case "Value 2": Action 2; [break;] Case "value n": action n; [break;] Default: default action [break;]}
Function
Scope
- Global / Window Scope Let a = 1; console.log (a);
- Function Scope Function Test () {Let B = 2; } test (); Console.log (B);
- Block Scope Function Test () {let C = 3; console.log (c); } test ();
Recursion: A function that calls himself.
Implementation:
- Replacing looping
- Fibonacci series
- Search & searches of data lists list & tree Examples: factorial function (n) {if (n === 0) Return 1; Return n * factorial (n -1); }
Data structure
Context
- Creation Phase in Global Scope Var name = undefined Name Function = fn (), which is its function itself.
- Hoisting window = Global Object This = Window
- Execution Phase Function makes Local Execution Context
Closures
- is a combination of function and lexical scope in the function.
- A function when it has access to Parent Scope, even though the parent scope has been completed. Example: Function NameFunction (Param1) {Return Namalocalfunction (Param2) {Function Body}} Declaration NAMEVARIABLE = NAMAFUNCTION (ARGUMENT1); NameVariable (Argument2);
Arrow Function
- Another form that is more concise than Function Expression. Example: Declaration Identifier = (Parameterlist Opt) => {Function Body} Example: Let number of numbers = (param1, param2) => {function body}; // Implicit Return
- This in the Arrow Function Arrow Function cannot be applied to this. Example: Declaration NameBject = Function (Param1) {this.Prperty1 = String; this.Property2 = () => {Function Body}; } Declaration NAMEVARIABLE = NEW NAMEBJECT ();
Higher Order function
Function that operates in other functions (in argument / return value). Javascript treats function as an object. Why: Abstraction, which is to simplify a program because the greater a program, the higher the complexity, the more confusing the programmer.
- Array.pototype.map ()
- Array.pototype.filter ()
- Array.pototype.reduce ()
Literal template
is a literal string that allows the expression in it. Can use: ''; ""; And ;
With backtick ( ) can make:
- Singleline String:
string text - Multiline String:
string text 1 string text 2 string text 3 - Embedded Expression:
string text ${expression} string text - HTML Fragments
- Expression Interpolation
- Tagged Templates:
- Escaping / sanitize html tags
- Translation & Internationalization (I18N)
- Styled Components
Destructuring Assignments
Expression on JS which makes us able to dismantle the value of Dr. Array / Property Dr. Object into a separate variable.
- Array const try = ['one', 'two', 'three']; const [a, b, c] = try;
- Object Const MHS = {Name: 'Ramadyan', Age: 25, Email: '[email protected]',}; const {name, age, email} = mhs;
- Function
- On return value
- On the arguments
Spread operator
Operators that break itlebles into single elements.
Rest parameter
Represents the argument in the function with unlimited jumalh into an array.
Synchronous vs Asynchronous
- Single-Thread vs Multi-Thread Environment Execution Task
- Blocking vs Non-blocking Ngoding Techniques (Input-Output Related)
- Synchronous vs Asynchronous Ngoding Engineering (http request related)
- Concurrent vs parallel environment execution task task (infra, hardware & virtualization related)
Callback
The function sent as a parameter in another function.
Example: Function NameFunction (NAMACALLBACK) {Declaration of Name Valis = Value; Callback (names); } NameFunction ((namevar) => {console.log (execution);});
Promise
Object that represents the success / failure of an async event in the future. Promise (Fulfilled / Rejected / Pending). Using Callback => Action (Resolve => Then / Reject => Catch / Pending => Finally).
Example: Declaration of Name Var1 = Value; const namevar2 = new promise ((resolve, reject) => {if (nameVAR1) {resolve (value);} else {reject (value);}}); namesvar2 .that ((response) => console.log (execution)) .catch ((response) => console.log (execution));
Ajax & Fetch
Fetch is a method of API JS to take resources from the network & return the complete promise (fullfilled) when there is a response that is available. Notation: Fetch (Resources, Init);
- Resources: URL / Request Object (Representation of Source Requests).
- Init: additional configuration in an object request (example: get, post, method, header, cache, etc.)
- Response: The results of Dr. Fetch in the form of Promise (Property & Method).
Async & Await
- A function that works asynchronous (through Event Loop),
- which produces (implicit) promise as a return value,
- But the way of writing the code using synchronous writing (standard).
A Async Function can have an Await keyword in it to temporarily stop the execution of its function while waiting for the promise to finish (resolve).
DOM (Document Object Model): Representation of the HTML element in the document becomes an object. Stored into Dom Tree.
DOM Benefits:
As an object -based programming interface that represents web documents.
DOM makes all components of web pages accessible & manipulated.
Components: HTML elements, attributes, text, etc.
Dom Tree
- Nodelist: A collection that contains any node.
- Htmlcollection: a collection that contains html elements and is live.
Hierarchy:
- Root Node (Document): The node that is the source of all other nodes in the dom.
- Parent Node: The node that is one level above another node. Ex: Body is Parent Dr. H1, P, DIV & A.
- Child node: The node that is one level below another node. Ex: H1 is Child Dr. Body.
Node type type/ node:
- Element
- attribute
- text
- CDATA Section
- Entity reference
- Entity
- Processing Instruction
- Comment
- Document
- Document Type
- document fragment
- Notation
Dom Selection
- Like using elements in HTML:
- GetlementByid () = produces elements.
- getelementsbytagname () = produces HTMLCollection.
- getelementsbyclassname () = produces HTMLCollection.
- Like wearing a selector on CSS:
- Querysector () = produces elements.
- Querysectorall () = produces nodelists.
Dom Manipulation
- Element Manipulation
- element.innerhtml = change the contents of the selected element.
- element.style. = Changing the style of the selected element.
- element.getattribute () = know the contents of the attribute.
- element.setttribute () = add attributes.
- element.removeattribute () = delete attributes.
- element.classlist:
- ADD () = adding class.
- Remove () = delete class.
- Toggle () = adding class if not there, delete if there is.
- Item () = checking class with index.
- Contains () = check the class in question.
- Replace () = Changing the class.
- Node manipulation
- document.createelement ()
- document.createtextnode ()
- node.ppendchild ()
- node.insertbefore ()
- parentnode.removechild ()
- parentnode.replacechild ()
Dom event
- Event Handler = can only run the last command.
- Inline html attribute
- Element Method on
- Addventlistener () = can be run many times.
Dom Traversal
- Parentnode = result node.
- Parentelement = the result of the element.
- Nextsibling = result node.
- NextelementSibling = the result of the element.
- ADCEFIBLING = Node results.
- PreviouselementSibling = the result of the element.
Default Prevent
- To prevent default action, use Method: event.preventdefault ();
Bubbling event
- When the event is run, it will run the event in Parent before going to Child. Using event.stoppropagate ();