After I create an object of primitive type on javascript console, it return undefine
for example
var john = new String("John");
undefined
But after continue to type
john
String {'John'}
CodePudding user response:
When typing code into the console, what it "reads back" to you after you press enter is the completion value of the last statement.
Variable declarations do not result in any completion value, so it reads back to you undefined
.
But, a statement referencing an existing identifier will give you the value in the identifier - which, in this case, is a String object.
(But, keep in mind that there's almost no good reason to use the primitive constructors - better to use var john = 'John';
)
CodePudding user response:
When you declare a variable and assign a value to it in console, it just sets the variable to the value you assigned it, it doesn't return anything.
So, if you open dev tools console and type:
let a = 1;
console will report undefined
if you then type a
into console and press return, 1
will be displayed as the value returned by variable name a.
What you described is normal behaviour for the console.