-1

It's hard to write console.log every time so I created a function for it,

function showi(input){
 console.log(input);
 }
 
showi('hello');

But the problem is that, It can't pass multiple arguments. For example:

function showi(input){
 console.log(input);
 }
 
let array1 = ['a', 'b', 'c'];

for(const [index, value] of array1.entries()){
 showi(index, value);
 }
 
//value will also print if I use console.log instead of `showi` function

How can I pass multiple values in showi function?

Rex5
  • 771
  • 9
  • 23
Ankit
  • 181
  • 2
  • 15

4 Answers4

2

If you find console.log is big to use then just assign it to a variable like logIt like below. In this case you can use this like how you are using console.log with single or any number of parameters

window.logIt=console.log;
logIt('hello');
let array1 = ['a', 'b', 'c'];
logIt('array1 : ',array1);
jafarbtech
  • 6,842
  • 1
  • 36
  • 55
  • Which one would be better `window.logIt` or making a function `logIt()`, What's the difference between them? – Ankit Sep 23 '19 at 06:39
  • 1
    @Ankit you can either use `window.logIt=console.log` or `logIt=console.log` to call `logIt` with any number of parameters like `console.log` – jafarbtech Sep 23 '19 at 06:44
  • How come `let print = document.write;` don't work the same way? – Ankit Oct 02 '19 at 16:14
1

Solution 1 - Use arguments

function showi(){
 console.log(...arguments);
}

Solution 2 - Use your own argument's name

function showi(...textToPrint){
 console.log(...textToPrint);
}
NCM
  • 358
  • 1
  • 4
0

Use the arguments object

function showi() {
    console.log(...arguments);
}

Or you can also use rest and spread operators in es6 -

function showi(...args) {
    console.log(...args);
}
Nithin Kumar Biliya
  • 2,763
  • 3
  • 34
  • 54
0

You can check if input is an array using Array.isArray and based on that show the content.

function showMe(input) {
  if (Array.isArray(input)) {
    console.log(...input);
  } else {
    console.log(input);
  }
}

showMe('sandip');
showMe(['a', 'b', 'c'])

Similarly you can add other checks.

Sandip Nirmal
  • 2,283
  • 21
  • 24