Why does browser returns undefined? [duplicate] - javascript

This question already has answers here:
assignment in javascript and the var keyword
(4 answers)
Closed 8 years ago.
I have found a phenomenon that confused me when I run simple javascript code from the browser console(Chrome & Firefox).
When I typed say
>var a = "a"
The browser will return a string
>"undefined"
but if I just typed
>a = "a"
The browser will return the string
>"a"
Why is this case?

If you write
alert(var a = 'a')
You get a syntax error, var is part of the javascript syntax, it doesn't return anything.
The a = 'a' portion, however, does return something.
You can do var a = b = c = d = 'e';
And the d = 'e' returns e, which gets fed into the c=d which is really c = 'e', etc. Once you get to the var it stops returning the value.
If you type var a; you get undefined. var a = 'b' is essentially just shorthand for var a; a = b;

The console is showing the result of the evaluated expression. Declaring the variable and assigning it at the same time with
var a = 'a'
does not return anything, so you get undefined. The result of just the assignment part
b = 'b'
returns the value, so you see it in the console.
var a = b = c = d = 'foo';
That returns undefined, although several variables have been set. The real purpose of the expression was to define the scope of the variables, setting the value was just a bit of shorthand.

Related

Chrome console object [duplicate]

This question already has answers here:
Why does JavaScript variable declaration at console results in "undefined" being printed?
(4 answers)
Closed 3 years ago.
When I write this in the console it's log undefined.
var obj = { first: 'lorem' };
But when I write this in the console it's log object.
var obj = { first: 'lorem' };
obj = { second: 'ipsum' };
Why is this happening?
The value you see being printed is the return value of the line of code you executed.
var obj = {first: 'lorem'};
returns undefined, whereas
obj = {second: 'ipsum'};
will return the object assigned to obj, hence {second: 'ipsum'} is printed.
This is why you can do things such as:
var a = b = 2;
Here the assignment of b = 2 will set b equal to 2, whilst also returning 2, thus setting a to 2
Anything you execute in the console, it will display the return value of it. When declaring and assigning variables, it will return undefined. When only assigning values to variables, that value is returned.

How to set one variable to another in same line without globalizing [duplicate]

This question already has answers here:
Assign two variables to the same value with one expression? [duplicate]
(5 answers)
Closed 4 years ago.
In this situation:
(function() {
const a = b = 10;
})();
console.log('a:', a); // ReferenceError: a is not defined
console.log('b:', b); // 10
The b gets defined as a var in the global scope. Is it possible o do this equals trick but make b a const as well in the same scope? Just like a?
You could set b as a parameter to your IIFE function. That way b won't be accessible outside of function scope.
(function(b) {
const a = b = 10;
console.log(b)
})();
console.log('b:', b);
I don't think so. This "trick" is possible because b = 10 returns 10. However, const b = 10 returns undefined.
Why not just use:
const a = 10;
const b = a;
?
It's not slower, and it's more readable, even if you could nest the assignments I don't really see a reason to do it

Javascript Function with String/Object attached to it [duplicate]

This question already has answers here:
Add method to string class
(6 answers)
Closed 4 years ago.
In javascript, I want to write a function which is called as follows:
var x = 'testString'
var y = 'anotherstring'
var z = 0
var result = x.aFunction(y, z)
function aFunction(y, z) {
...
}
This is the first time I am attempting this, my question is how can I get and use the value of x in the function aFunction, without actually referring to the declared variable.
I tried looking for this but I cannot find anything. If there is a post specifically for this that anyone knows about, please let me know.
Thanks
You need to use String.prototype.aFunction so that you can add a custom function aFunction() to the prototype of String such that it can be invoked by a string variable. Also this.toString() inside the prototype function will give you the value of the x variable (calling string)
var x = 'testString'
var y = 'anotherstring'
var z = 0
String.prototype.aFunction = function(y, z){
console.log(this.toString());
return y+z;
}
var result = x.aFunction(y, z);
console.log(result);

How to find out the variable type in JavaScript? [duplicate]

This question already has answers here:
Finding Variable Type in JavaScript
(12 answers)
Closed 6 years ago.
I'm new to JavaScript and is writing a simple website using Netbeans. Since JavaScript is dynamically typed language, I was wondering how can I find out the type of a variable in situations where I am unsure of.
For example, how can I find out the variable type of emailAddress or domainPart in the code below?
function getEmailAndDomainParts(){
var emailAddress = document.getElementById("EmailAddress").value;
var emailPart = emailAddress.substring(0, emailAddress.indexOf("#"));
var domainPart = emailAddress.substring(emailAddress.indexOf("#") + 1);
document.getElementById("Email").value = emailPart;
document.getElementById("Domain").value = domainPart;
}
// test data
var myArray = ['a', 'b', 'c'];
// the usual typeof isn't very useful
alert(typeof myArray);
// this instance of method tests the array
// to see if it is an instance of the 'Array'
// constructor, which it is!
alert(myArray instanceof Array)
click here
you can use typeof:
The typeof operator returns a string indicating the type of the unevaluated operand
As pointed out in the comments, you cannot check the type of your variable but you can check the type of your variable's value using typeof():
x = "hello";
y = 123;
z = true;
console.log(typeof(x)); //Will return "string"
console.log(typeof(y)); //Will return "number"
console.log(typeof(z)); //Will return "boolean"

In javascript, what do multiple equal signs mean? [duplicate]

This question already has answers here:
Javascript a=b=c statements
(6 answers)
Closed 9 years ago.
I saw this code somewhere, but what does it mean? (all a, b, c are defined previously)
var a = b = c;
It quickly assigns multiple variables to a single value.
In your example, a and b are now equal set to the value of c.
It's also often used for a mass assign of null to clean up.
a = b = c = d = null;
Assign c to b.
Assign b to a.
So if I say var a = b = 1;
>>> var a = b = 1;
undefined
>>> a
1
>>> b
1
This means a, b and c are the same reference.
For example:
var c = {hello: "world"};
var a = b = c;
// now all three variables are the same object
It's a shorthand for:
var a;
var b;
b=c;
a=b;
It's meant as a combination of assigning the same value to two or more other variables, and declaring these variables in local scope at the same time.
You can also use this syntax independently of the var declaration:
var a;
var b;
a=b=c;

Categories

Resources