Below is a simple code
var stop = true;
console.log(stop);
In console is show like this:
true
<. undefined // what this undefined, is it some reference.
I am wondering what is undefined
Every statement in JavaScript returns something.
When you enter console.log(true), true will be printed out and undefined will be returned.
undefined is one of the seven types in JavaScript that means something has been declared but not assigned a value.
undefined is the return value of console.log statements.
It's the return value of the console.log function.
The true that you see is a side-effect.
Related
This question already has answers here:
Chrome/Firefox console.log always appends a line saying 'undefined'
(9 answers)
Closed 8 years ago.
console.log("hi") gives
undefined
hi
console.log(1+1) gives
undefined
2
Whether it's a string or integer calculation, I get undefined then the correct answer.
Why do I get the undefined message? Is there a good way to avoid it?
The console will print the result of evaluating an expression. The result of evaluating console.log() is undefined since console.log does not explicitly return something. It has the side effect of printing to the console.
You can observe the same behaviour with many expressions:
> var x = 1;
undefined;
A variable declaration does not produce a value so again undefined is printed to the console.
As a counter-example, expressions containing mathematical operators do produce a value which is printed to the console instead of undefined:
> 2 + 2;
4
The undefined is the return value of console.log(). This is the standard behavior of Chrome, Safari, and Firefox JS Consoles.
The console shows the return value of your input. console.log() doesn't return anything, so undefined.
You could just type directly into the console to get the result.
This is because console.log() does not return a value (i.e. returns undefined). The result of whatever you entered to the console is first printed to the console, then a bit later the message from console.log reaches the console and is printed as well.
If a browser does not show the undefined, it means it has noticed that your console input only prints to the console, and so skips showing the result.
It returns the value of console.log(...).
Define two functions like this and you'll see why.
function functionA() {
return 1;
}
function functionB() {
return;
}
The functionB() returns undefined.
I'm using V8 (via Chrome) to test this. In Javascript, a code block evaluates to whatever the last value in the code block evaluates to. I wanted to see what would happen if I assigned a variable to an empty code block.
// example of code block evaluating to last value in the block
> {1;2;3};
3
// This won't work, it returns an empty object,
// not an empty code block.
> var emptyBlock = {};
> emptyBlock;
Object object
Eventually, I figured out how to specify that I want an empty code block, not an empty object.
> {;}
undefined
> {;;}
undefined
> {;;;;;}
undefined
Okay, beautiful, so an empty code block resolves to undefined. Right?
Not exactly!
> var emptyBlock = {;}
Uncaught SyntaxError: Unexpected token ';'
?! Now it is unclear whether the empty code block actually returns undefined, or something else entirely is at play. If it merely did evaluate to undefined, we would expect the above assignment to work.
Does anyone know why this is happening?
Eventually, I figured out how to specify that I want an empty code block, not an empty object.
This is largely determined by what is before the {, not after it.
Since you have an = before it, you are in a context where { is the start of an object literal. A ; is not allowed where a property name is expected, so the code errors.
You can't assign blocks. They aren't values, they are parts of the code structure.
Both of these seem demonstrably false. var codeBlockValue = {1;2;3}; for example works perfectly fine, and sets codeBlockValue to 3
No, it doesn't:
var codeBlockValue = {1;2;3};
Hey pretty simple task I am trying to do.... I'm trying to take the text in my textbox and assign it to a var. When I run without the var I am getting text but if I assign it to a var I get undefined. Could someone explain this to me as it is very confusing to me?
TL;DR: It does not.
You can see content of your variable test, il will output the same thing as before. In fact it is the variable assignement that returns the undefined you see here.
For instance:
var test = 'Hello' // => undefined
test // => 'Hello'
Another case is printing your variable with console.log. If you do so, the return value will be undefined but the output will be your variable content (Hello here).
console.log(test) // return: undefined / print: Hello
What's returning undefined is the statement itself that you entered into the console, NOT the value of var text.
To see that console.log(text) or simply type text in the console.
I know undefined is a property of the global object.
A variable that has not been assigned a value is of type undefined.
But I wonder why i get undefined of assigned values in a if else condition.
Here is my code
var a;
var b;
a =5
b =10;
if((b>a))
console.log("a is greater than b")
else
console.log("b is greater than a")
and I get
undefined
b is greater than a
why it return undefined first even both variable is already assigned
Your only issue is that you're executing this in an interactive console. The console immediately evaluates every expression and shows you its value. This is so you can experiment and immediately see each result. The console prints a return value for every single line, even if that line has no return value.
If you type 'aaa', this expression evaluates to the value 'aaa', which the console will show you immediately. The expression 40 + 2 results in the value 42 being printed. The statement if (true); results in no value (if is not an expression and returns no value), so undefined is printed.
That's where your undefined comes from; not from anywhere within your code.
In javascript, you have to write every snippet as a function. Otherwise, every snippet will return undefined, Look at the below code
if(true){}
Even this will return undefined;
Make sure you follow JS best practices.
Better rewrite the code as below,
function check(a,b){
if(a > b){
return 'a is greater than b';
}else {
return 'b is greater than a';
}
}
console.log(check(1,2));
Revert to me if you need more clarifications.
Happy coding!!
Steps:
Type in console of chrome:
[].anyName
This works Fine.
But when we type
{}.anyName
It gives error.
Because {} without context - ie in an assignment or as a parameter to a function, are just a block statement, and they return undefined.
When you enter {} to the console and click enter the result is undefined, as this was a block statement that just finished it's work and without an explicit return statement (you can't return from a block statement) returns undefined by default.
When you enter [] to the console and click enter the result is [], as this creates a new array.
[] this is an array which you can add a methods to the array prototype so when you type [].anyTime it's mean that you want to search in the array prototype for an anyTime method which does not exist so this will give you an undefined , but you can't access {}.anyTime in this way cause it is an object and you can access object like this var obj={}; obj.anyTime