function bark() { console.log('woof woof') } bark() // 'woof woof' function Dog(age, weight) { this.species = 'Canis Familiaris' this.age = age this.weight = weight this.bark = bark //bark() function from above } // Spot and Bingo are 'instance-objects' of Dog var Spot = new Dog(8, 65) var Bingo = new Dog(10, 70) console.log(Spot.species)// 'Canis Familiaris' console.log(Spot); //Dog { // species: 'Canis Familiaris', // age: 8, // weight: 65, // bark: [Function: bark] //} // bark is a 'method' of Dog Bingo.bark()// 'woof woof' console.log(Object.getPrototypeOf(Bingo) === Bingo.prototype); // false console.log(Object.getPrototypeOf(Bingo) === Dog.prototype); // true console.log(Bingo.__proto__ === Dog.prototype); //true console.log(Dog.prototype);//Dog{} console.log(Bingo.prototype);//undefined console.log(Bingo.__proto__);//Dog{} Dog.prototype.jump = function() { console.log('hop hop') } Spot.jump() //hop hop Bingo.jump() // =>hop hop console.log(Spot); //Dog { // species: 'Canis Familiaris', // age: 8, // weight: 65, // bark: [Function: bark] //} console.log("***Dog***"); var insect = {legs: 6} // insect.__proto__ === Object.prototype // insect.hasOwnProperty === Object.prototype.hasOwnProperty console.log(Object.getPrototypeOf(insect) === Object.prototype); //true console.log(Object.getPrototypeOf(insect));//{} console.log(Object.prototype); //{} console.log(insect.__proto__); //{} console.log(insect.prototype); //undefined console.log(insect.hasOwnProperty('legs')) // => true var Max = new Dog() console.log(Max.__proto__ === Dog.prototype); //true console.log(Max.__proto__); //Dog { jump: [Function] } console.log(Max.prototype); //undefined console.log(Dog.prototype.__proto__ === Object.prototype);//true console.log(Dog.prototype.__proto__);//{} console.log(Dog.prototype);//Dog { jump: [Function] } console.log(Dog.prototype.hasOwnProperty('jump'));//true console.log(Dog.prototype.hasOwnProperty('bark'));//false console.log(Dog.__proto__ === Object.prototype);//false console.log(Dog.__proto__);//[Function] console.log(Max.hasOwnProperty('weight')); // => true function Foo() { this.something = 'blah' } console.log(Foo.prototype.__proto__ === Object.prototype); //true console.log(Foo.prototype.__proto__); //{} console.log(Foo.prototype); //Foo {} console.log(Foo.__proto__); //[Function] console.log(Foo.hasOwnProperty('name')); // => true console.log(Foo.name); //Foo console.log(Foo.hasOwnProperty('something')); // false =>set on instance-object not on the function console.log(Foo.something); //undefined console.log(Foo.prototype.someting); //undefined console.log("---------------"); var Max = new Dog(); console.log(Max.prototype); //undefined console.log(Max.__proto__ === Dog.prototype); //true console.log(Max.__proto__); //Dog { jump: [Function] } console.log(Dog.prototype); //Dog { jump: [Function] } console.log(Dog.__proto__ === Function.prototype); //true console.log(Dog.__proto__); //[Function] console.log(Function.prototype); //[Function] console.log(Function.__proto__ === Function.prototype); //true console.log(Function.__proto__); //[Function] console.log(Function.prototype); //[Function] console.log("^^^^^^^^^^^^^"); var crab = {legs: 6}; console.log(crab.prototype); //undefined console.log(crab.__proto__ === Object.prototype); //true console.log(crab.__proto__); //{} console.log(Object.prototype); //{} console.log(Object.__proto__ === Function.prototype); //true => Object() is a function object console.log(Object.__proto__); //[Function] console.log(Function.prototype); //[Function] console.log(Function.__proto__ === Function.prototype); //true console.log(Function.__proto__); //[Function] console.log(Function.prototype); //[Function] console.log(Function.prototype.__proto__ === Object.prototype); //true =>prototype is just an object console.log(Object.prototype.__proto__);//null console.log(Dog.prototype);//Dog { jump: [Function] } console.log("############"); function Labrador(furColor, age, weight) { this.furColor = furColor this.breed = 'labrador' Dog.call(this, age, weight) } Labrador.prototype = Object.create(Dog.prototype) console.log(Labrador.prototype);//Dog{} var Fido = new Labrador('white', 4, 41) console.log(Fido.__proto__); //Dog{} Fido.bark(); //woof woof Fido.jump(); //hop hop console.log(Fido.furColor);//white console.log(Fido.breed);//labrador console.log(Fido.age);//4 console.log(Fido.constructor); console.log(String.prototype.__proto__ === Object.prototype);//true console.log(String.hasOwnProperty('length')); //true var myCar = new Object(); myCar.make = 'Ford'; myCar.model = 'Mustang'; myCar.year = 1969; console.log(JSON.stringify(myCar)); //{"make":"Ford","model":"Mustang","year":1969} console.log(myCar); //{ make: 'Ford', model: 'Mustang', year: 1969 } console.log("***new Object()***"); console.log(myCar.prorotype); //undefined console.log(myCar.__proto__); //{} console.log("***new Object()***"); user = { x:5, sayHi: function() { console.log("Hello, this user object"); }, y:20 }; console.log(JSON.stringify(user)); //{"x":5,"y":20} console.log(user); //{ x: 5, sayHi: [Function: sayHi], y: 20 } console.log("***object lateral***"); console.log(user.prorotype); //undefined console.log(user.__proto__); //{} console.log("***object lateral***"); console.log("***object.create()***"); var userTom = Object.create(user); console.log(userTom.y); // 20 userTom.sayHi(); //Hello, this is user object console.log(user.prorotype); //undefined console.log(userTom.__proto__); //{ x: 5, sayHi: [Function: sayHi], y: 20 } console.log(Object.getPrototypeOf(userTom) === user.prototype); //false console.log(Object.getPrototypeOf(userTom));//{ x: 5, sayHi: [Function: sayHi], y: 20 } //console.log(userTom instanceof user); //TypeError: Right-hand side of 'instanceof' is not callable console.log(userTom);//{} console.log("***object.create()***"); console.log("****new function()****") function greeting(name) { this.names = name; this.isAdmin = false; console.log("hello, this is greeting function") }; greeting(); //hello, this is greeting function console.log(greeting.names); //undefined console.log(greeting.name); //greeting console.log(greeting);//[Function: greeting] var g_obj = new greeting("John"); //hello this is greeting function console.log(greeting.prototype); //greeting {} console.log(g_obj.__proto__); //greeting {} console.log(Object.getPrototypeOf(g_obj) === g_obj.prototype); //false console.log(Object.getPrototypeOf(g_obj) === greeting.prototype); //true console.log(g_obj.__proto__ === greeting.prototype); //true console.log(g_obj instanceof greeting); //true console.log(g_obj); //greeting { names: 'John', isAdmin: false } console.log(g_obj.prototype); //undefined console.log("****new function()****") function MyConstructor(name) { this.names = name; this.isAdmin = false; MyConstructor.fun1 = function(){}; console.log("hello, this is greeting function too"); this.fun3 = function(){console.log("hello, this is fun1")}; }; //console.log(MyConstructor.fun1()); //TypeError: MyConstructor.fun1 is not a function console.log(MyConstructor.fun1); //undefined var myObj = new MyConstructor("John"); myObj.fun1(); //undefined console.log(Object.getPrototypeOf(myObj) === myObj.prototype); // false console.log(Object.getPrototypeOf(myObj) === MyConstructor.prototype); // true console.log(myObj.__proto__ === MyConstructor.prototype); // true function MyConstructor(){ }; console.log(MyConstructor.prototype) // MyConstructor {} MyConstructor.prototype.func2 = function(){ }; console.log(MyConstructor); // [Function: MyConstructor] console.log(MyConstructor.prototype); // MyConstructor { func2: [Function] } //MyConstructor.func2(); // TypeError: MyConstructor.func2 is not a function console.log("****"); function Person() { this.name = "irshad" }; var eve = new Person("Eve"); console.log(Person); console.log(Person.prototype); console.log("*******"); console.log(eve.__proto__); //it will print the same as above console.log("*********"); eve.address = "ald"; console.log(eve.__proto__ == Person.prototype); //true var car = { model: 'bmw', color: 'red', price: 2000 } console.log(JSON.stringify(car)); car.type = 'manual'; // dynamic property console.log(JSON.stringify(car)); function Car(model, color) { this.model = model; this.color = color; } var c1 = new Car('BMW', 'red'); console.log(c1); console.log(typeof(c1)); // Function declaration. function showFavoriteIceCream() { const favIceCream = 'chocolate'; console.log(`My favorite ice cream is ${favIceCream}`); } showFavoriteIceCream(); // Let's assign a property. showFavoriteIceCream.flavours = ['chocolate', 'vanilla', 'strawberry']; // Let's log the showFavoriteIceCream function. console.log(showFavoriteIceCream); var obj = {prop1: 'prop1Value', prop2: 'prop2Value', child: {childProp1: 'childProp1Value'}} console.log(obj) console.log(JSON.stringify({ x: 5, y: 6 })); // expected output: "{"x":5,"y":6}" console.log(JSON.stringify([new Number(3), new String('false'), new Boolean(false)])); // expected output: "[3,"false",false]" console.log(JSON.stringify({ x: [10, undefined, function(){}, Symbol('')] })); // expected output: "{"x":[10,null,null,null]}" console.log(JSON.stringify(new Date(2006, 0, 2, 15, 4, 5))); // expected output: ""2006-01-02T15:04:05.000Z""
Write, Run & Share Javascript code online using OneCompiler's JS online compiler for free. It's one of the robust, feature-rich online compilers for Javascript language. Getting started with the OneCompiler's Javascript editor is easy and fast. The editor shows sample boilerplate code when you choose language as Javascript and start coding.
Javascript(JS) is a object-oriented programming language which adhere to ECMA Script Standards. Javascript is required to design the behaviour of the web pages.
var readline = require('readline');
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
rl.on('line', function(line){
console.log("Hello, " + line);
});
Keyword | Description | Scope |
---|---|---|
var | Var is used to declare variables(old way of declaring variables) | Function or global scope |
let | let is also used to declare variables(new way) | Global or block Scope |
const | const is used to declare const values. Once the value is assigned, it can not be modified | Global or block Scope |
let greetings = `Hello ${name}`
const msg = `
hello
world!
`
An array is a collection of items or values.
let arrayName = [value1, value2,..etc];
// or
let arrayName = new Array("value1","value2",..etc);
let mobiles = ["iPhone", "Samsung", "Pixel"];
// accessing an array
console.log(mobiles[0]);
// changing an array element
mobiles[3] = "Nokia";
Arrow Functions helps developers to write code in concise way, it’s introduced in ES6.
Arrow functions can be written in multiple ways. Below are couple of ways to use arrow function but it can be written in many other ways as well.
() => expression
const numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
const squaresOfEvenNumbers = numbers.filter(ele => ele % 2 == 0)
.map(ele => ele ** 2);
console.log(squaresOfEvenNumbers);
let [firstName, lastName] = ['Foo', 'Bar']
let {firstName, lastName} = {
firstName: 'Foo',
lastName: 'Bar'
}
const {
title,
firstName,
lastName,
...rest
} = record;
//Object spread
const post = {
...options,
type: "new"
}
//array spread
const users = [
...adminUsers,
...normalUsers
]
function greetings({ name = 'Foo' } = {}) { //Defaulting name to Foo
console.log(`Hello ${name}!`);
}
greet() // Hello Foo
greet({ name: 'Bar' }) // Hi Bar
IF is used to execute a block of code based on a condition.
if(condition){
// code
}
Else part is used to execute the block of code when the condition fails.
if(condition){
// code
} else {
// code
}
Switch is used to replace nested If-Else statements.
switch(condition){
case 'value1' :
//code
[break;]
case 'value2' :
//code
[break;]
.......
default :
//code
[break;]
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
ES6 introduced classes along with OOPS concepts in JS. Class is similar to a function which you can think like kind of template which will get called when ever you initialize class.
class className {
constructor() { ... } //Mandatory Class method
method1() { ... }
method2() { ... }
...
}
class Mobile {
constructor(model) {
this.name = model;
}
}
mbl = new Mobile("iPhone");