Skip to content

Instantly share code, notes, and snippets.

@bosz
Last active March 28, 2022 10:19
Show Gist options
  • Save bosz/aa904b7dc82a9986ff62771aca868353 to your computer and use it in GitHub Desktop.
Save bosz/aa904b7dc82a9986ff62771aca868353 to your computer and use it in GitHub Desktop.
Basics of js; variable, operations, loops, arrays
// Variables
var a = 1;
let b = 2;
const c = 3;
const pi = 3.17;
console.log(a, b, c, pi);
// Operations
let sum = a + b;
let sub = a - b;
let div = a / b;
let mutl = a * b;
let mod = a % b;
// Conditions
// if, else, elif, else if
if (a > b) {
console.log('a is greater than b');
}else {
console.log('a is not greater than b');
}
if (a == 2) {
console.log('a is equalt to 2');
}else if(a == 4) {
console.log('a is 4')
}else {
console.log('a is neither 2 nor 4');
}
// switch
switch(a){
case 2:
console.log('In case 2');
break;
case 4:
console.log('In case 4');
break;
default:
console.log('in default')
}
// Loops
for($i = 0; $i <= 10; $i++) {
console.log('ForLoop ' + $i);
}
$age = 0;
while ($age <= 5) {
console.log('I am a child ' + $age + ' years');
$age++;
}
// Arrays
let names = ['john', 'peter', 'samuel'];
let familyArray = names.map((name, key) => {
if (name == 'john') {
return name;
}else {
let newName = name + ' ' + 'Ndelli';
return newName;
}
});
console.log(familyArray);
// Functions
function sumFxn(a, b) {
return a + b;
}
let sumFunctionReturnValue = sumFxn(1, 5);
console.log('My sum function returned ' + sumFunctionReturnValue);
// Objects
let student = {
id: 1,
name: 'Ndelli Peter',
class: 'DR Intern 1',
subject: 'Javascript',
learn: function(){
console.log('I am learning');
},
changeName: function(newName) {
this.name = newName;
},
setMark: function(mark) {
this.mark = mark;
}
}
console.clear();
// student.learn();
// console.log(student.name);
console.log(student);
student.changeName('Martin');
student.setMark(44);
console.log(student);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment