Why does the console print undefined when I assign a variable? - javascript

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.

Related

can anyone explain what's happening here How its printing world ? #javascript [duplicate]

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.

Output the variable name to console

I am wanting to output the variable and variable name to console by just using the variable. This must be simple but I can't figure out how to do it. So I want the output I'd get from the below code but without hardcoding the variable name.
const myVar = 10
console.log('myVar',myVar)
//OUTPUT myVar, 10
SO just to make it clear I want the variable name to be programatically created from the variable NOT hardcoded 'myVar' as I want to automate this.
I HAVE TRIED
'[myVar]'
//doesn't work
myVar.name
//doesn't work
I HAVE BEEN ASKED TO EXPLAIN WHY
I am sick of writing
console.log('myVar',myVar')
I would like to create a function that takes in a variable to outputs what I want to the console.
I TRIED
const myVar = 10
const outVar = (myVar) =>{
console.log({somethingHere}, myVar)
}
//somethingHere is a placeholder for the correct syntax
outvar(myVar)
//expected Outcome myVar, 10
There is no way to know the variable name. You can make it as an object with the shorthand property names which as close as you can get to what you want. It is great for debugging.
const myVar = 10
const myVar2 = 4
console.log({ myVar })
console.log({ myVar, myVar2 })

Console.log vs printing out variable

How come when I use console.log to print out a value in the console window for a string it is not in quotes, but when I just output the variable in the console, it prints quotes. Is there any particular reason for this?
var test = “hello”;
test;
Output : “hello”
Console.log(test);
Output: hello
Well , see like this.
var test = "hello";
test; // This is object in self and what is it,
// it is a string in literal
// Output comes only from debugger , when you input direct.
// test; this line have no output to the console
// Output : "hello" NO
//console.log(test); // console.log already print string (in native/string is output) but also print objects.
// Output: hello YES
console.log( test + " this is the test")
// See output it is a very clear
// hello this is the test
The default behavior is for strings to be represented along with quotes in the console.
a = 'hi';
a
// returns "hi"
The console api is different and an exception.
console.log(object [, object, ...])
Displays a message in the console. Pass one or more objects to this method. Each object is evaluated and concatenated into a space-delimited string.
So it returns a space-delimited, concatenated string. Which means it will always be a string. Since its always a string, we can do away with the quotes. I suppose the console devs let it be that way to make the point that console.log() will always return the same type (string). Adding quotes might imply the possibility that it could return other things, so it seems to be a UX thing for the console.
Javascript is dynamically typed which means a variable can store any type of value at any time. If you call a variable storing a string (which is test in your case) it prints out the value "Hello" indicating its a string and returns a string datatype, which is preety straightforward. But numbers can also be strings like var a = "5". On the other hand console.log() simply prints the value inside the variable and by default returns undefined.
var a = "hello";
// To check the return type of variable a which is string
console.log(typeof(a));
// To check the return type of console.log() which is undefined
console.log(typeof(console.log(a)));

Javascript what does <. undefined in console after console.log()

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.

Why do some node properties give me undefined when I log them?

I have an endpoint in my node app and I am doing the following...
app.put('/api/authentication', function(req,res){
console.log(global.ff); //undefined
console.log(req.ff); //undefined
console.log(blah); //throws error
});
I don't understand why the first two give me 'undefined'. They should throw an error as I have never once declared them. But it's as if node.js has magically declared them and that is why they do not throw an error like the last one...Can someone explain?
There's a difference between undefined and "non-existent"
In the first two lines, global and req are existing variables, they just don't have the keys that you're asking for - so you get undefined.
However, blah simply doesn't exist - node has no place to even ask for the key you're looking for. Note that you can test for undefined as per this answer.
If you had defined blah above, but set no value to it (var blah;), you'd get another undefined error there instead.
global and req are existing variables.
Asking for a key in existing variable returns it, and if not found - tells that the key is undefined.
Next code will return undefined as well:
var blah = "";
console.log(blah.blah);

Categories