Javascript Compressed
Javascript Compressed
fundamental
Course Target
1. Understanding Javascript Concept
2. Understanding Variable
3. Understanding Data Type
4. Understanding Array
5. Understanding Conditional
6. Understanding Looping
7. Understanding Type Conversion
8. Understanding Function
9. Understanding Object
10. Understanding Class
Javascript introduction
JavaScript is a programming language used in website
development to be more dynamic and interactive. if
previously you only know HTML and CSS, with JavaScript
can increase functionality on web pages.
or
compiler intepreter
what is javascript used?
intepreter
let's start use Javascript
Download and Install NodeJS
Node.js is Ryan Dahl's platform for running applications
JavaScript-based web which was introduced in 2009. With
this platform, you can run JavaScript from the server side.
var
Block Scope
const name
is an old-school variable declaration
function Scope
can Redeclared
let
is a modern variable declaration.
can Reassign
can Reassign
can Hoisting Block Scope
var name
let name
Introduction Hoisting
hoisting is appointment a variable before called
Block Scope vs Function scope
Block scope is an activity limit that is delimited by brackets
curly braces in a for loop or if condition or a variable.
Block scope = let, const
Global variables are visible from any function (unless shadowed by locals).
Data Type
Data Type
Data types are the types of data that we can store in variables
Primitive data types can store only one value at a time.
Non-primitive data types can store more than one value at a time
Object
Operator
Operators are symbols that are used to perform operations
on values and variables.
NOT operators
Array
Array
Array is a structured data type that functions to store
a number of data or elements where each element has an index
declaration :
let students = new Array()
let students = []
Array Method
push add array elements at the following index
extracts a part of a string and returns the extracted part in a new string
CharAt BackTik
array.CharAt(index) ` this code ${expression} `
Conditional
If Else Statement
If is a key for branching, which means we can execute certain
program code when a certain condition is met.
In general, this loop is divided into two. Namely, calculated loops and
uncountable loops.
Init statement
will be executed only once at the start before the loop
Post statement
will be executed after each loop
While Do While
while (condition) { do {
// block looping // block looping
} }
while(condition)
Arrays Loop
Let arr = [‘sony’, ‘nikon’, ‘Fujifilm’]
Function declaration
Function expression
Functions Declarations
The function keyword goes first, then goes the name of the
function, then a list of parameters between the parentheses
(comma-separated, empty in the example above) and finally the
code of the function, also named “the function body”, between
curly braces
function sayHello() {
console.log("Hello Enigmanians ! ");
}
Functions Expression
is a function stored in a variable.
Parameters
Variables that are written in brackets when the function is
created, used to hold the value passed during the function sent
function sayHello(name) {
console.log("Hello " + name);
}
Argument
The value passed to the parameter when the function is called
sayHello('Enigma') ;
Lexical scope
The lexical scope is the scope that is read when the JavaScript code is
going through the compile process, or often called compile-time. This
lexical scope regulates in which scope we have to look for a variable.
example
var x = 2;
var add = function() {
var y = 1; return x + y;
};
Arrow Function
ES6 introduced a new function called arrow function. Arrow
functions are similar to normal functions in behavior, but
differ in the way they are written. As the name implies,
functions are defined using arrows or fat arrow =>
IIFE vs SIAF
SIAF (Self Invoking Anonymus Function) is
when there is a the variable insidethe
function is not named then the variable is
not accessible outside
Object
Object
An object can be created with figure brackets {…} with an optional list of
properties. A property is a “key: value” pair, where key is a string (also called a
“property name”), and value can be anything.
We can imagine an object as a cabinet with signed files. Every piece of data is
stored in its file by the key. It’s easy to find a file by its name or add/remove a file.
// bad
const user = new Object(); // object structural syntax
// good
const user = {}; // object literal syntax
Accessing Object
const user = {
fullName: 'Ramadhan',
age: 22,
isActive: true
startRun: function(){
console.log(“1 2 3 go!”)
}
}
user.fullName = 'Rohkmah'
Meanwhile, if the property with the specified key name is not found, then a
new property will be added to the object.
user.email = 'user@gmail.com'
We can also delete properties on objects by using the delete keyword
delete user.email;
Keyword this
The this keyword here refers to the owner of the function itself.
const student = {
name : 'Anis' ,
address: { city: 'Dewata' }
console.log(`${this.name}`)
}
The call, apply, and bind methods are used to access data from other objects that differ in context (this)
function getInfo(address) {
console.log(this.address.city);
console.log(`My address is ${address.city}, ${address.province}`); // error
}
getInfo.call(student, student.address);
getInfo.apply(student, [student.address]); ]
Immutable Object
By default objects in JavaScript are mutable
If we create a new variable and we immediately assign an object, the new
variable only copies that object by reference, does not copy its value
There are 2 ways we can copy object references and their values:
class Car {
constructor(brand) {
this.brand = brand;
}
engineStart() {
console.log(`${this.brand} ${this.name} engine started`);
}
}
const corolla = new Car('Toyota');
corolla.engineStart() // Toyota Corolla engine started
Class static method
A method that can be used without having to initialize the object first
Model.hello()
Inheritance (derivative)
Using the extend keyword
class model is the lower class or child of the car
The model class accesses whatever is in the parent
class Hewan {
berjalan(name) {
console.log(`${name} berjalan`);
}
}
class Hewan {
constructor(name) {
this.name = name;
}
class Kucing extends Hewan {
constructor() {
super('Kucing');
}
}
Thank You