What does the [whatever]:[number] construct mean in the developer console? [duplicate] - javascript

This question already has answers here:
What does ':' (colon) do in JavaScript?
(11 answers)
Closed 9 months ago.
When I was using the developer console in the browser, I accidentally entered a construction like whatewer:42
The console in response output a number after the colon. I don't understand why such a construction is needed. If you use it anywhere else, you get an error.

If you use it anywhere else, you get an error.
No, you don't. It's a labeled statement.
function test() {
whatever: 42;
console.log('No error');
}
test();

Related

Error in Overriding alert with console.log function in javascript [duplicate]

This question already has answers here:
Uncaught TypeError: Illegal invocation in JavaScript
(2 answers)
Closed 6 years ago.
I am new to javascript. Due to certain reasons, I need to override windows.alert function by console.log function. For that, I have written following code,
window.alert = console.log;
alert('abc'); // gives TypeError : Illegal invocation.
I don't know what's wrong I am doing here. As per my knowledge, it's possible to modify javascript function reference with another function object.
Edit
Many have downvoted my question and given me reference to similar question, but
my problem is different. I want to suppress the warning of datatable grid library in jquery. That's why I want to replace alert by console.log or other function.
Hope this should work...
alert("msg");
function alert(msg){
console.log(msg);
}
This should do
var alert = (...params) => console.log(...params);

Result of syntax in JS [duplicate]

This question already has answers here:
How is this valid javascript, and what purpose does it serve?
(2 answers)
Closed 7 years ago.
I'm switching from C++ to JS.
//if ( condition )
{
instruction1;
instruction2;
}
In C/C++ commenting if before the block would simply execute the block unconditionally.
Could I use it the same way in JS? Or would it create an unnamed object which is never used?
A very quick way of testing:
{
alert("foo");
}
http://jsfiddle.net/4obksb0s/
Yes - it appears to run fine.

what is an illegal invocation typeerror in javascript [duplicate]

This question already has answers here:
Create shortcut to console.log() in Chrome
(4 answers)
Uncaught TypeError: Illegal invocation in JavaScript
(2 answers)
Illegal Invocation error when console.log passed in a function
(1 answer)
Closed 8 years ago.
I have javascript code that raises an alert if it's being run in the browser, but which I don't want to raise alerts when I run unit tests.
I tried to solve this by having a line
if( allowAlerts === false ){
alert = console.log;
}
but when I then run
alert("This bad thing happened");
I get back
TypeError: Illegal invocation
directly reassigning alert was a kludgey solution, and I can easily solve the problem in other ways, but I've never come across an illegal invocation error before and was hoping someone could explain what it means.
The console.log function needs its calling context to be the console.
Use
alert = console.log.bind(console);
Or if you want to be compatible with old IE :
alert = function(){ console.log.apply(console, arguments) };

Return void(0); vs return; interrupting functions [duplicate]

This question already has answers here:
What does "javascript:void(0)" mean?
(14 answers)
Closed 9 years ago.
I ran across some code that interrupts a function return void(0);.
I believe that is being used to return undefined but that can be done simply by writing return;.
Does return void(0); serve an additional purpose, or is this just two different ways to interrupt a function?
return void(0); doesn't do anything special. It simply returns undefined, albeit in a very silly way. It's probably a case of the original developer not understanding JavaScript fully.
It's just another way to return undefined. See https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/void

How to validate a JSON String JQuery [duplicate]

This question already has answers here:
AJAX: Check if a string is JSON?
(8 answers)
Closed 9 years ago.
I tried that:
var c = $.parseJSON(something here)
and I control that:
c === undefined
This works however it throws error while trying to parse an invalid JSON string. I don't want it throw that error.
Any advices?
It's generally considered bad practice to suppress/ignore errors, instead why not use a try-catch block to capture the exception and do something with it:
try {
var c = $.parseJSON(something here);
}
catch (err) {
// Do something about the exception here
}
If you really don't need to do anything about the exception at least put a comment to that effect in your try-catch block, it'll make your code more readable when you come back to it later.

Categories