// function calcTime(city, offset) { // // create Date object for current location // var d = new Date(); // // get UTC time in msec // var utc = d.getTime(); // // create new Date object for different city // // using supplied offset // var nd = new Date(utc + (3600000*offset)); // // return time as a string // return "The local time in " + city + " is " + nd.toLocaleString(); // } // var x=calcTime("Canada",); // console.log(x) // function changeTimezone() { // let date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0)); // console.log('Given IST datetime: ' + date); // let intlDateObj = new Intl.DateTimeFormat('en-US', { // timeZone: "America/New_York" // }); // let usaTime = intlDateObj.format(date); // console.log('USA date: ' + usaTime); // } // changeTimezone() // function a(datetime,exp_tz) { // var date = new Date(datetime); // var localTime = date.getTime(); // var a=exp_tz; // // get local timezone offset and convert to milliseconds // var localOffset = date.getTimezoneOffset() * 60000; // // obtain the UTC time in milliseconds // var utc = localTime + localOffset; // if(a=="GMT"){ // let newDateTime = utc + (3600000 * -5.5); // let convertedDateTime = new Date(newDateTime); // return convertedDateTime.toLocaleString(); // } // else if(a=="IST"){ // newDateTime = utc + (3600000 * 1); // let convertedDateTime = new Date(newDateTime); // return convertedDateTime.toLocaleString(); // } // else if(a=="PST"){ // newDateTime = utc + (3600000 * -0.5); // let convertedDateTime = new Date(newDateTime); // return convertedDateTime.toLocaleString(); // } // else{ // let convertedDateTime = new Date(newDateTime); // return convertedDateTime.toLocaleString(); // } // } // var x=a(); // console.log(x) // const date1 = new Date('August 19, 1975 23:15:30 GMT+07:00'); // const date2 = new Date('August 19, 1975 23:15:30 GMT-02:00'); // console.log(date1.getTimezoneOffset()); // // expected output: your local timezone offset in minutes // // (eg -120). NOT the timezone offset of the date object. // console.log(date1.getTimezoneOffset() === date2.getTimezoneOffset()); // // expected output: true // function changeTimezone() { // let date = new Date(Date.UTC(2021, 5, 28, 3, 0, 0)); // console.log('Date in India: ' + date); // let formatter = new Intl.DateTimeFormat('en-US', { timeZone: "America/Denver" }); // let usDate = formatter.format(date); // console.log('Date in USA: ' + usDate); // } // changeTimezone() // function a(datetime,exp_tz) { // var date = new Date(datetime); // var localTime = date.getTime(); // var a=exp_tz; // // get local timezone offset and convert to milliseconds // var localOffset = date.getTimezoneOffset() * 60000; // // obtain the UTC time in milliseconds // var utc = localTime + localOffset; // if(a=="GMT"){ // var newDateTime = utc + (3600000 * -5.5); // var convertedDateTime = new Date(newDateTime); // return convertedDateTime.toLocaleString(); // } // else if(a=="IST"){ // var newDateTime = utc + (3600000 * 1); // var convertedDateTime = new Date(newDateTime); // return convertedDateTime.toLocaleString(); // } // else if(a=="PST"){ // var newDateTime = utc + (3600000 * -0.5); // var convertedDateTime = new Date(newDateTime); // return convertedDateTime.toLocaleString(); // } // else{ // var convertedDateTime = new Date(newDateTime); // return convertedDateTime.toLocaleString(); // } // } // var x=a(); // console.log(x) // const a=10; // console.log(a); // const o={"name":"abc","age":28}; // // console.log(o) // // console.log(o.name) // o.age=30; // console.log(o.age) // const date = new Date(); // let day = date.getDate(); // let month = date.getMonth() + 1; // let year = date.getFullYear(); // let currentDate = `${month}/${day}/${year}`; // console.log(currentDate); // "17-6-2022" // var date1 = new Date(); // function t(Invoice_Generation_Date) { // // var date1 = new Date(Invoice_Generation_Date); // var diffDays = ((date1.getMonth()+1)+"/"+date1.getDate()+"/"+date1.getFullYear()+" "+date1.getHours()+":"+date1.getMinutes()+":"+date1.getSeconds()); // return diffDays; // } // var t1=t(); // console.log(t1) // var m1 = new Date('21-11-2022'); // var m2 = new Date('22-11-2022'); // var m3 = m2(m1,'minutes'); // var m4 = m2(m1,'h'); // var numdays = Math.floor(m3 / 1440); // var numhours = Math.floor((m3 % 1440) / 60); // var numminutes = Math.floor((m3 % 1440) % 60); // console.log(numdays + " day(s) " + numhours +"h " + numminutes +"m"); // function dateDiff( str1, str2 ) { // var diff = Date.parse( str2 ) - Date.parse( str1 ); // return isNaN( diff ) ? NaN : { // diff : diff, // ms : Math.floor( diff % 1000 ), // s : Math.floor( diff / 1000 % 60 ), // m : Math.floor( diff / 60000 % 60 ), // h : Math.floor( diff / 3600000 % 24 ), // d : Math.floor( diff / 86400000 ) // }; // } // var r=dateDiff('21-11-2022','23-11-2022'); // console.log(r); // var a=new Date(); // console.log(a); // function t(s1,s2) // { // // var diff =(s2.getTime() -s1.getTime()) / 1000; // var diff =(s2 -s1) / 1000; // diff /= 60; // return Math.floor(diff)+" Minutes"; // } // var t1=t('21-11-2022 ','23-11-2022'); // console.log(t1) // function(s1,s2) // { // var diff =((Current_Date_Exp.getTime()) - (Invoice_Generation_Date.getTime())) / 1000; // diff /= 60; // return Math.floor(diff)+" Minutes"; // } // function diff_minutes(dt2, dt1) // { // var diff =(dt2.getTime() - dt1.getTime()) / 1000; // diff /= 60; // // return Math.abs(Math.round(diff))+" Minutes"; // return Math.abs(diff)+" Minutes"; // } // dt1 = new Date(2022,10,2); // dt2 = new Date(2022,10,3); // console.log(diff_minutes(dt1, dt2));
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");