This question already has answers here:
javascript if giving "undefined" in console [duplicate]
(2 answers)
Closed 3 years ago.
I am following a book on Javascript and the example doesn't seem to return any value. I don't have much experience with OOP though.
I believe this is a programmatical way to create classes, but it returns undefined any way
What do you make of this?
Thank you in advance
This is expected, variable declarations always return undefined.
E.g., let a = 0, var b = 0, const c = 0.
This doesn't mean a, b, and c are undefined though.
Likewise for your example, createClass and Book aren't undefined, as seen by your console.log.
Related
This question already has answers here:
How is almost everything in Javascript an object?
(6 answers)
Closed 5 months ago.
I tested this today and I don't understand the behaviour. For me, that should be an error.
const a = 1;
a.test = 1;
console.log(a)
console.log(a.test)
Someone have the answer ?
you can define properties pretty much anything, but in this case you are assigning it to a number which doesn't actually hold properties.
This question already has answers here:
console.log() shows the changed value of a variable before the value actually changes
(7 answers)
Is Chrome’s JavaScript console lazy about evaluating objects?
(7 answers)
console.log() async or sync?
(3 answers)
Weird behavior with objects & console.log [duplicate]
(2 answers)
Closed 7 months ago.
Hey mods the other question you linked had nothing to do with mine. Even on that questions page it says that the behavior doesn't exist anymore.
To preface, this isn't an issue with the variable i in the closures all being the same although it might look like it is.
The following code has a weird behavior. Basically the code runs the body of a for loop 3 times where it creates a set timeout that modifies and logs the same array.
const obj = {a:1,b:{c:2}};
console.log(obj);
obj.b.c = 3;
I expected when expanded the log would show that c was 2, but it actually shows that c is 3.
Why does this happen?
(I already answered below)
What is happening is that when you console.log a object only the first level of properties are stored before expanding in the console(clicking the > on an object property and seeing its properties). Think about the alternative, you deep copy every object that is logged. Then because of properties like __proto__ and constructor you would have to copy many many items. And if the object happens to have some reference to window you would practically have to take a memory snapshot.
This question already has answers here:
Why does JavaScript variable declaration at console results in "undefined" being printed?
(4 answers)
Closed 4 years ago.
In chrome Developer tools, when I type
a = 3 logs 3 but
var a = 3 logs undefined.
Why does the first statement return 3 but not the second one?
Because that's the way it is.
A statement beginning var is a declaration. Declarations don't, in and of themselves, have a value. They tell the computer to do something (to create a variable, optionally with some initial value).
But assignment expressions are different. a = b evaluates to (or "has") the new value of a, in order to allow chaining, such as a = b = c = d.
Could they have made it so that var a = b was an expression and evaluated to something? Sure, probably. But it would hold absolutely no useful value to butcher the language grammar in such a manner.
Read up about statements and expressions in programming languages.
Because undefined is the result of the var statement.
Statements don't actually have a "result" that can be used in your code, but a program has a final result, and your single line of code in the console is evaluated as a program.
This question already has answers here:
Multiple assignment in JavaScript? What does `[ a, b, c ] = [ 1, 2, 3 ]` mean?
(4 answers)
Closed 5 years ago.
Take the following line of code
const [component] = router.getMatchedComponents({ ...to })
Could anyone advise what the square brackets around component means here? I have tried to google this but struggling to find an answer
It's called Destructuring assignment, and it's used to unpack the values of an array and assign them to new variables.
So here in your code:
const [component] = router.getMatchedComponents({ ...to })
You are assigning to the component variable the first element held in the array that will be returned by router.getMatchedComponents({...to}), where to is an array-like structure turned into object using the spread operation.
This question already has answers here:
Why does JavaScript variable declaration at console results in "undefined" being printed?
(4 answers)
Closed 8 years ago.
Here I define a variable:
var number = Math.round(Math.random() * 10);
When I plug it into the Chrome DevTools JavaScript Console, I get a very weird error: undefined. I have never seen this error before, and I don't see how the variable is undefined.
Is there something I'm doing wrong?
(I'm sure this is one of those in-plain-sight issues.)
That's right. This expression returns undefined - because var doesn't return anything.
But if you type number and press enter you will get the result.