0

So after looking around a bit, I've found console.log() can blow up IE9. So I can't leave any in my code, that being the case, what could I do here?

I have my data_box which displays some text the user has inputted. But, if there is no text, I want to call showMsg(), which creates a new box with some text, 'No Text Here'.

(data_box.length) ? console.log('') : showMsg();

What alternative can I replace 'console.log('')' with to tell that ternary function to do nothing if data_box exists?

Daft
  • 10,277
  • 15
  • 63
  • 105
  • 2
    why not `if(data_box.length) { showMsg()}` ? – Luca Rainone Apr 11 '14 at 13:34
  • Unless I'm missing something, it seems like an `if` statement would do the trick. `if(data_box.length > 0){ showMsg(); }`. – ajm Apr 11 '14 at 13:34
  • 1
    You can just mock out console.log to avoid the error instead. Check http://stackoverflow.com/questions/1146309/common-idiom-to-avoid-ie-throw-error-console-is-undefined – Evan Knowles Apr 11 '14 at 13:34
  • I want showMsg() to be called if data_box doesn't exsist. So wouldn't if(data_box.length) { showMsg()} be incorrect? – Daft Apr 11 '14 at 13:38
  • possible duplicate of ['console' is undefined error for Internet Explorer](http://stackoverflow.com/questions/3326650/console-is-undefined-error-for-internet-explorer) – Benjamin Gruenbaum Apr 11 '14 at 13:40
  • 1
    @Daft well, sorry just negate the condition: `if(!data_box.length){ showMsg()}` – Luca Rainone Apr 11 '14 at 13:42
  • Oh yeah, sorry. I was just wondering if I had made a mistake in my original code. Cheers chumkiu! – Daft Apr 11 '14 at 13:45
  • @chumkiu if you wanna throw you answer down below, i'll select it as right. – Daft Apr 11 '14 at 13:50
  • This is a moot question. You are using `console.log` for a useless purpose here anyway. Don't use a ternary operator if you don't need an else statement, use a simple if statement. – Ennui Apr 11 '14 at 13:54
  • Thank's for the incredibly helpful response Ennui. I'm just lucky not everyone here is as unbelievably helpful as you... – Daft Apr 11 '14 at 14:01

4 Answers4

2

There’s no logical reason to use console.log for this purpose.

Simply you can avoid the ternary operation if you don’t need an else (in some case is more short than ternary operator).

if(!data_box.length){ showMsg()}

If you want absolutely the ternary operator you can do this:

data_box.length? null : showMsg();

Or if you want absolutely a function (I don’t see any valid reason… however it’s just an exercise), you can declare a function doNothing in this way:

function doNothing() {}

and

(data_box.length)? doNothing() : showMsg();

or without function delcaration:

data_box.length? (function() {})() : showMsg();

or again:

data_box.length || showMsg();

and so on… :-)

Luca Rainone
  • 16,138
  • 2
  • 38
  • 52
1

The simplest thing to do is to write your own console.log function and use it if the browser doesn't provide one.

window.console = window.console || { log: function () { } };

… but it would be a better idea to strip your debugging routines from your code before it goes to production.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Well I'm not really using it for debugging purposes, it's more used here to just 'do nothing' if the ternary operator returns true. – Daft Apr 11 '14 at 13:37
  • 1
    I'd just use a regular `if` statement for that. Much cleaner and more self-documenting. – Quentin Apr 11 '14 at 13:40
0

I'd have given you few lines of code to make it work, but following blog has done great job in addressing the issue. Do read this

http://patik.com/blog/complete-cross-browser-console-log/
Bikas
  • 2,709
  • 14
  • 32
0

As for myself, i've stopped using directly console.log(). It is too much character !

I have my own log() function :

function log() {
    var arg = Array.prototype.slice.call(arguments);

    if (console && console.log) {
        console.log.apply(console, arg);
    } else {
        for (var i = 0; i < arg.length; i++) alert(arg[i]);
    }
}

Then you can call it :

log('test', {}, ['test', 'hello']);

In ie, it will pop 3 alert : 'test', [object Object], 'test,hello'


Edit

To "do nothing if true", you can do that :

(data_box.length) || showMsg();

But really, it is better to use an if:

if(!data_box.length) showMsg();
Community
  • 1
  • 1
Karl-André Gagnon
  • 33,662
  • 5
  • 50
  • 75