Youcef Madadi
Web and game development teacher
DISCOVER JS
// -Declarations
// var X
var name;
var anotherName;
var Name3;
var 4thName; // wrong declaration
var diffrent_name;
var §erfez; // wrong declaration
var $name;
var _name;
// Types
var
// Numbers
num = 5,
float = 6.3,
// strings
text = "message",
text2 = 'message2',
// bool
conditionT = true,
conditionF = false,
// undefined and null
empty1= null,
empty2= undefined;
// Constants
const Trainer= "Youcef Madadi";
const Students= 30;
Trainer = "Someone Else" /* Uncaught TypeError: Assignment
* to constant variable.*/
const Assistant; /* Uncaught SyntaxError: Missing initializer
* in const declaration*/
// Arithmetic operations
x = 5 + 6; // addition
y = 7 - 3; // minus
z = 4 * 4; // multiplication
a = 7 / 2; // division
b = 4 ** 2; // power
b = b % 2; // Modulus
a++; // Increment
b--; // Decrement
Arithmetic operations
// String manipulation
name = "Youcef";
text = "Hello new user : " + name;
message= `From ${name} : ${text}`
String manipulation
// Logical operations
c = (x >= y); // ( x > y )
d = (y <= z); // ( y < z )
e = (z == x);
f = (5 == "5");
g = (5 === "5");
h = (5 !== "5");
i = (name == text);
j = (!g);
k = (c && e);
l = (c || e);
Logical operations
// Binary operations
m = (7 & 6);
n = (10 | 9);
o = (~z);
p = (x ^ z);
q = (5 >> 1);
Binary operations
// Conditions
if (/* condition1 */) {
/* Instractions 1 */
} else if (/* condition2 */) { // optional
/* Instractions 2 */
} else { // optional
/* Instractions 3 */
}
Conditions
// question mark operation (ternary)
// ( condition ) ? (true Result) : (false Result)
var max = a>b ? a : b;
Conditional (ternary) operator
// Switch
switch(Bounadem){
case Trainer:
console.log("You can Teach go on");
break;
case Assistant:
console.log("Well he gotta take care of the trainer");
break;
case CoAssistant:
console.log("You can just sell della3")
break;
default:
console.log("You are a trainee");
break;
}
Switch case
// loop 1
while (/* condition */) {
/* Instractions to repeat */
}
Loops (while)
// loop 2
do{
/* Instractions to repeat */
} while (/* condition */) ;
Loops (do while)
// loop 3
for (var i = 0; i < N; i++) {
/* Instractions to repeat */
}
Loops (for)
// loop 4
for (var i = N; i > 0; i--) {
/* Instractions to repeat */
}
Loops (for reverse)
// Array Declarations
var list=[];
var Notes=[12,14,8,16,4,9,13,12,"Absent"];
Declaration
// Array Assignment
list[0]="Youcef";
list[4]="Abdelhak";
Assignment
// Declaring a function
function Max(a, b) {
if (a > b) return a;
else return b;
}
Declaring a function
// Declaring a function in a variable
var a=prompt("enter a"),
b=prompt("enter b");
console.log( Max( a , b ) );
Calling a function ( invoking )
// Declaring a function in a variable
var maxf = function(a,b){
if(a>b) return a;
else return b;
}
Declaring a function in a variable
// name of a function
console.log( Max.name , maxf.name );
Name of a function
// Sending function as a parameter
function call(Caller,a,b){
return Caller(a,b);
}
Sending a function as a parameter
// not setting any arguments and getting away with it
function Max(){
var max=-Infinity;
for(var i=0; i < arguments.length ; i++){
max=Math.max(max,arguments[i]);
}
return max;
}
Arguments for the function
// Declaring an object
var person={
first_name:"Youcef",
"last name":"Madadi",
age:23
}
Declaring an object
// Accessing data in objects
console.log(
person.first_name,
person["last name"],
person["age"]
)
Accessing Data in an Object
// Getting Keys in an object
for(var key in person){
console.log(key);
}
Get Objects properties
// Getting Keys of an object
Object.getOwnPropertyNames(person)
Object.keys(person)
Getting Keys
// puting functions in objects (methods)
var person1 = {
first_name: "Youcef",
"last name": "Madadi",
age: 25,
todoList:[],
ToDo: function (Do) {
todoList.push(Do);
return `I'm Going to do : ${Do}`;
},
};
Methods
// this key word to access other members
var person2 = {
first_name: "Youcef",
"last name": "Madadi",
age: 25,
intreduce: function () {
console.log(
`I'm ${this.first_name} ${this["last name"]} and i'm ${this.age} years old`
);
},
};
this key word
// Genrate Objects (classes first form)
function Person(first_name, last_name, age) {
this.first_name = first_name;
this.last_name = last_name;
this.age = age;
this.fullName = function () {
return this.first_name + " " + this.last_name;
};
}
var p1 = new Person("Youcef", "Madadi", 25);
var p2 = new Person("Someone", "Else", 22);
Constructing Object (new key word)
// Getting Keys in an object
Peoples=[p1,p2,p3...]
console.log(Peoples.length)
Length
// Lambda functions
var sum = function (a, b) {
return a + b;
};
// we could write the above example as:
sum = (a, b) => a + b;
// or
sum = (a, b) => {
return a + b;
};
var max = (a, b) => (a > b ? a : b),
fact = (n) => (n > 1 ? n * fact(n - 1) : 1);
By Youcef Madadi