├── hello.js ├── .DS_Store ├── functions.js ├── ifelse.js ├── classes.js ├── index.html ├── polymorphism.js ├── prototype.js ├── polymm.js ├── Comments&Variables.js ├── forloop.js ├── encapsulation.js ├── abstraction.js ├── inheritance.js ├── polym.js ├── switchcase.js ├── whileloop.js ├── arrays.js ├── prototyp.js └── Operators.js /hello.js: -------------------------------------------------------------------------------- 1 | console.log('Hello World...!'); -------------------------------------------------------------------------------- /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Raghav-Pal/JavaScript/HEAD/.DS_Store -------------------------------------------------------------------------------- /functions.js: -------------------------------------------------------------------------------- 1 | 2 | // function funcName(param1, param2){ 3 | // //statments 4 | // } 5 | 6 | function add(a, b){ 7 | // console.log (a + b); 8 | return (a + b) 9 | } 10 | 11 | let result = add(2, 4); 12 | console.log (result); 13 | -------------------------------------------------------------------------------- /ifelse.js: -------------------------------------------------------------------------------- 1 | 2 | let age = 18; 3 | 4 | if(age > 18){ 5 | console.log ('Congrats, You are eligible to get driving licence'); 6 | } 7 | else if(age == 18){ 8 | console.log ('Congrats! You just became eligible to get driving license. Go Drive!') 9 | } 10 | else{ 11 | console.log('Sorry! You are not eligible to get driving licence') 12 | } 13 | -------------------------------------------------------------------------------- /classes.js: -------------------------------------------------------------------------------- 1 | class Employee{ 2 | 3 | constructor(name, id){ 4 | this.name = name; 5 | this.id = id 6 | } 7 | } 8 | 9 | let emp1 = new Employee('John', 1001); 10 | let emp2 = new Employee('Peter', 1002); 11 | 12 | console.log (emp1.name); 13 | console.log (emp1.id); 14 | 15 | console.log (emp2.name); 16 | console.log (emp2.id); 17 | 18 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 | 6 | 7 |This is a demo paragraph
11 | 14 | 15 | -------------------------------------------------------------------------------- /polymorphism.js: -------------------------------------------------------------------------------- 1 | class Animal{ 2 | 3 | constructor(name){ 4 | this.name = name 5 | } 6 | 7 | eats(){ 8 | console.log (this.name+' eats food'); 9 | } 10 | } 11 | 12 | class Alligator extends Animal{ 13 | 14 | eats(){ 15 | super.eats(); 16 | console.log (this.name+' eats fishes'); 17 | } 18 | } 19 | 20 | let murphy = new Alligator('Murphy'); 21 | murphy.eats(); -------------------------------------------------------------------------------- /prototype.js: -------------------------------------------------------------------------------- 1 | let EmpDetails = function(name, age){ 2 | this.name = name; 3 | this.age = age; 4 | 5 | }; 6 | 7 | EmpDetails.prototype.getName = function(){ 8 | return this.name; 9 | }; 10 | 11 | EmpDetails.prototype.getAge = function(){ 12 | return this.age; 13 | }; 14 | 15 | 16 | let emp1 = new EmpDetails('John', 30); 17 | let emp2 = new EmpDetails('Peter', 40); 18 | console.log (emp1.getName()); 19 | console.log (emp2.getAge()); -------------------------------------------------------------------------------- /polymm.js: -------------------------------------------------------------------------------- 1 | class Animal{ 2 | 3 | constructor(name){ 4 | this.name = name; 5 | } 6 | 7 | Eats(){ 8 | console.log (this.name+' eats food'); 9 | } 10 | } 11 | 12 | class Alligator extends Animal{ 13 | 14 | constructor(name){ 15 | super(name); 16 | } 17 | Eats(){ 18 | super.Eats(); 19 | console.log (this.name+' eats fish'); 20 | } 21 | } 22 | 23 | let murphy = new Alligator('murphy'); 24 | murphy.Eats(); -------------------------------------------------------------------------------- /Comments&Variables.js: -------------------------------------------------------------------------------- 1 | // This is a single line comment 2 | 3 | 4 | /* 5 | This is a 6 | multi-line 7 | comment 8 | */ 9 | 10 | 11 | /********* Variables **********/ 12 | 13 | let x; 14 | x = 10; 15 | 16 | let x = 10; 17 | 18 | let message = 'Hello'; 19 | message = 'Welcome'; 20 | 21 | let x,y; 22 | x=10; 23 | y=20; 24 | 25 | let x=10, y=20; 26 | 27 | let name; 28 | let Name; 29 | 30 | let firstName = 'Raghav'; 31 | let lastName = 'Pal'; 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /forloop.js: -------------------------------------------------------------------------------- 1 | let i; 2 | 3 | for(i = 1; i <= 5; i++){ 4 | console.log ('value of i is : '+i); 5 | } 6 | 7 | let fruits = ['Apple', 'Banana', 'Grapes']; 8 | let x; 9 | for (x in fruits){ 10 | console.log (fruits[x]) 11 | } 12 | 13 | for(x of fruits){ 14 | console.log (x); 15 | } 16 | 17 | 18 | let count = 11; 19 | 20 | while(count <= 10){ 21 | console.log ('Count '+count) 22 | count=count+1; 23 | } 24 | 25 | count = 11; 26 | do{ 27 | console.log ('Count... '+count) 28 | count=count+1; 29 | }while(count <= 10) -------------------------------------------------------------------------------- /encapsulation.js: -------------------------------------------------------------------------------- 1 | class Employee{ 2 | 3 | setEmpDetails(name, id, phoneNo){ 4 | this.name = name; 5 | this.id = id; 6 | this.phoneNo = phoneNo; 7 | } 8 | 9 | getEmpName(){ 10 | return this.name; 11 | } 12 | getEmpId(){ 13 | return this.id; 14 | } 15 | getEmpPhoneNo(){ 16 | return this.phoneNo; 17 | } 18 | 19 | } 20 | 21 | let emp1 = new Employee(); 22 | 23 | emp1.setEmpDetails('John', 1001, 987890000); 24 | console.log (emp1.getEmpName()); 25 | console.log (emp1.getEmpId()); 26 | console.log (emp1.getEmpPhoneNo()); 27 | -------------------------------------------------------------------------------- /abstraction.js: -------------------------------------------------------------------------------- 1 | function Employee(name, age, baseSalary){ 2 | this.name = name; 3 | this.age = age; 4 | this.baseSalary = baseSalary; 5 | 6 | let monthlyBonus = 1500; 7 | 8 | let calculateFinalSalary = function(){ 9 | let finalSalary = baseSalary + monthlyBonus; 10 | console.log ('Final Salary is : '+finalSalary); 11 | } 12 | 13 | this.getEmpDetails = function (){ 14 | console.log ('Name : '+this.name+' | Age : '+this.age); 15 | calculateFinalSalary(); 16 | } 17 | } 18 | 19 | let emp1 = new Employee('John', 30, 2000); 20 | emp1.getEmpDetails(); 21 | 22 | console.log ('abcd'); -------------------------------------------------------------------------------- /inheritance.js: -------------------------------------------------------------------------------- 1 | class Car{ 2 | 3 | setName(name){ 4 | this.name = name; 5 | } 6 | 7 | startEngine(){ 8 | console.log ('Engine started for '+this.name); 9 | } 10 | 11 | stopEngine(){ 12 | console.log ('Engine stopped for '+this.name); 13 | } 14 | 15 | } 16 | 17 | class Toyota extends Car{ 18 | 19 | topSpeed(speed){ 20 | console.log ('Top speed for '+this.name+' is '+speed); 21 | } 22 | 23 | startEngine(){ 24 | console.log ('Engine started from child class'); 25 | } 26 | } 27 | 28 | let myCar = new Toyota(); 29 | myCar.setName('Camry'); 30 | myCar.startEngine(); 31 | myCar.stopEngine(); 32 | myCar.topSpeed(200); -------------------------------------------------------------------------------- /polym.js: -------------------------------------------------------------------------------- 1 | function Person(name, age){ 2 | this.name = name; 3 | this.age =age; 4 | 5 | } 6 | Person.prototype.getInfo = function(){ 7 | return 'Name : '+this.name+' | Age : '+this.age; 8 | }; 9 | 10 | function Employee(name, age, salary){ 11 | this.name = name; 12 | this.age = age; 13 | this.salary = salary; 14 | } 15 | 16 | 17 | Employee.prototype.getInfo = function(){ 18 | return 'Name : '+this.name+' | Age : '+this.age+' | salary : '+this.salary; 19 | }; 20 | 21 | Employee.prototype = new Person(); 22 | 23 | let per1 = new Person('John', 30); 24 | let emp1 = new Employee('Sarah', 40, 5000); 25 | 26 | console.log (per1.getInfo()); 27 | console.log (emp1.getInfo()); -------------------------------------------------------------------------------- /switchcase.js: -------------------------------------------------------------------------------- 1 | let starRating = 5; 2 | 3 | switch(starRating){ 4 | case 1: 5 | console.log ('Very Bad'); 6 | break; 7 | case 2: 8 | console.log ('Bad'); 9 | break; 10 | case 3: 11 | console.log ('Average'); 12 | break; 13 | case 4: 14 | console.log ('Good'); 15 | break; 16 | case 5: 17 | console.log ('Excellent'); 18 | break; 19 | default: 20 | console.log ('Enter a valid integer between 1 and 5'); 21 | } 22 | 23 | 24 | let month = 5; 25 | 26 | switch(month){ 27 | case 1: 28 | console.log ('Month is January'); 29 | break; 30 | case 2: 31 | console.log ('Month is Feb'); 32 | break; 33 | } 34 | 35 | 36 | -------------------------------------------------------------------------------- /whileloop.js: -------------------------------------------------------------------------------- 1 | let counter=11; 2 | 3 | while(counter <= 10){ 4 | 5 | console.log('counter : '+counter); 6 | counter=counter+1; 7 | } 8 | 9 | counter = 11 10 | do{ 11 | console.log('counter value is : '+counter); 12 | counter++; 13 | }while(counter <= 10) 14 | 15 | 16 | let numbers = [1,2,3,4,5]; 17 | 18 | let fruits = ['Apple', 'Banana', 'Grapes']; 19 | 20 | let cars = new Array('Toyota', 'Mercedes', 'Honda'); 21 | 22 | console.log (numbers); 23 | console.log (fruits); 24 | console.log (cars[1]); 25 | 26 | cars[1] = 'Ford'; 27 | console.log (cars[1]); 28 | 29 | // let person = {firstName:'John', lastName:'Doe', age:25}; 30 | // console.log (person); 31 | // console.log (person.firstName); 32 | 33 | console.log (cars.length) 34 | console.log (cars); 35 | cars.sort; 36 | console.log (cars); 37 | 38 | fruits.push('cherry') 39 | console.log (fruits) 40 | console.log (typeof cars) 41 | console.log (fruits instanceof Array) 42 | 43 | 44 | -------------------------------------------------------------------------------- /arrays.js: -------------------------------------------------------------------------------- 1 | // let numbers = [1, 2, 3, 4, 5]; 2 | // console.log (numbers); 3 | 4 | // let fruits = ['Apple', 'Banana', 'Grapes']; 5 | // console.log (fruits); 6 | 7 | // let cars = new Array('Ford', 'Toyota', 'Mercedes'); 8 | // console.log (cars); 9 | 10 | // console.log (cars[1]); 11 | // cars[1] = 'Honda'; 12 | // console.log (cars[1]); 13 | // console.log (cars); 14 | 15 | // fruits.push('cherry'); 16 | // console.log (fruits); 17 | // console.log (fruits.length); 18 | 19 | // let x; 20 | // for(x in fruits){ 21 | // console.log (fruits[x]); 22 | // } 23 | 24 | // for(x of fruits){ 25 | // console.log(x) 26 | // } 27 | 28 | 29 | 30 | let fruits = ['Apple', 'Banana', 'Grapes']; 31 | console.log (fruits); 32 | 33 | fruits.pop() 34 | console.log (fruits); 35 | 36 | fruits.shift(); 37 | console.log (fruits); 38 | 39 | fruits.push('Cherry'); 40 | console.log (fruits); 41 | 42 | fruits.unshift('Lemon'); 43 | console.log (fruits); 44 | 45 | delete fruits[1]; 46 | console.log (fruits); 47 | 48 | fruits[1] = 'Orange'; 49 | console.log (fruits); 50 | 51 | fruits.splice(1,2); 52 | console.log (fruits); 53 | 54 | fruits.splice(2,0,'Orange', 'Cherry') 55 | console.log (fruits); 56 | 57 | let citrusFruits = fruits.slice(0,2) 58 | console.log (fruits); 59 | console.log (citrusFruits); 60 | 61 | let evenNumbers = [2, 4, 6]; 62 | let oddNumbers = [1,3,5]; 63 | let primeNumbers = [2,3,5]; 64 | 65 | let numbers = evenNumbers.concat(oddNumbers, primeNumbers); 66 | console.log (numbers); 67 | 68 | ABCD 69 | 123 70 | 71 | 72 | 73 | 74 | 75 | -------------------------------------------------------------------------------- /prototyp.js: -------------------------------------------------------------------------------- 1 | let X = function(name, age){ 2 | 3 | this.name = name; 4 | this.age = age; 5 | 6 | }; 7 | 8 | X.prototype.getName = function(){ 9 | return this.name 10 | }; 11 | 12 | let x1 = new X('John', 20); 13 | console.log (x1.getName()); 14 | 15 | 16 | let getEmpDetails = function(name, age){ 17 | this.name = name; 18 | this.age = age; 19 | } 20 | 21 | getEmpDetails.prototype.getAge = function(){ 22 | return this.age 23 | } 24 | 25 | getEmpDetails.prototype.getName = function(){ 26 | return this.name 27 | } 28 | 29 | let emp1 = new getEmpDetails('John1', 30); 30 | 31 | console.log (emp1.getName()); 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | function EmpDetails(name, age){ 41 | this.name = name; 42 | this.age = age; 43 | } 44 | 45 | EmpDetails.prototype.getName = function(){ 46 | return this.name; 47 | } 48 | 49 | EmpDetails.prototype.getAge = function(){ 50 | return this.age; 51 | } 52 | 53 | let empOne = new EmpDetails('Kevin', 40); 54 | let empTwo = new EmpDetails('Sarah', 30); 55 | 56 | console.log (empOne.getName()); 57 | console.log (empTwo.getName()); 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | let EmpData = function(name, age){ 71 | this.name = name; 72 | this.age = age; 73 | 74 | 75 | }; 76 | 77 | EmpData.prototype.getName = function(){ 78 | return this.name; 79 | }; 80 | EmpData.prototype.getAge = function(){ 81 | return this.age; 82 | }; 83 | 84 | let emp1One = new EmpData('AA', 20); 85 | let emp2Two = new EmpData('BB', 30); 86 | 87 | console.log (emp1One.getName()); 88 | console.log (emp2Two.getAge()); -------------------------------------------------------------------------------- /Operators.js: -------------------------------------------------------------------------------- 1 | /******* Arithmetic Operators ********/ 2 | 3 | // let x = 5; 4 | // let y = 2; 5 | 6 | // let result = x ** y; 7 | // console.log('Result is : '+result) 8 | 9 | // let a = 10 10 | // let q = 2 * --a 11 | 12 | // console.log('Value of q is : '+q) 13 | // console.log('Value of a is : '+a) 14 | 15 | 16 | /******* Assignment Operators ********/ 17 | 18 | // let x = 10; 19 | // ///x = 10 + 20; 20 | 21 | // x *= 5; // x = x * 5 22 | // console.log('value of x is : '+x) 23 | 24 | 25 | /******** Comparison Operations *********/ 26 | 27 | // let x = 11; 28 | // let y = 12; 29 | // console.log(x >= y); 30 | 31 | /* 32 | Ternary Operator : like if-then-else 33 | takes 3 arguments 34 | condition ? value if true : value if false 35 | */ 36 | 37 | // let result = x > y ? x : y 38 | // console.log(result); 39 | 40 | /******** Logical Operators *********/ 41 | 42 | // let A = true; 43 | // let B = false; 44 | 45 | // console.log((10>5) || (6<3)); 46 | 47 | 48 | /******** Type Operators *********/ 49 | 50 | 51 | // let firstName = 'Raghav'; 52 | 53 | // console.log (typeof firstName); 54 | // console.log (typeof 123.34); 55 | // console.log (typeof true); 56 | 57 | // let lastName = new String('Pal'); 58 | // let num1 = new Number(123) 59 | 60 | // console.log (num1 instanceof Number); 61 | 62 | 63 | // console.log (typeof "Raghav"); // Returns "string" 64 | // console.log (typeof 123); // Returns "number" 65 | // console.log (typeof false); // Returns "boolean" 66 | // console.log (typeof NaN); // Returns "number" 67 | // console.log (typeof [1,2,3,4]); // Returns "object" 68 | // console.log (typeof {name:'Peter', age:25} ); // Returns "object" 69 | // console.log (typeof new Date()); // Returns "object" 70 | // console.log (typeof null); // Returns "object" 71 | // console.log (typeof function () {}); // Returns "function" 72 | // console.log (typeof myClass); // Returns "undefined" 73 | 74 | 75 | // console.log ('********* Bitwise Operators ***********'); 76 | 77 | // let A = 1; 78 | // let B = 2; 79 | // console.log (A & B); 80 | // console.log (A | B); 81 | 82 | // console.log (3^5); 83 | // console.log (~5); 84 | 85 | 86 | --------------------------------------------------------------------------------