Why does my var become undefined yet being set in the previous function?

javascript object property exists but is undefined
javascript function return undefined
javascript function undefined but exists
cannot read property of undefined
javascript undefined error
javascript variable undefined after assignment
undefined javascript
if undefined javascript

Currently I'm trying to simulate a login / register form and I'm currently running in to a problem I can't seem to find an solution to where it says my "username" is undefined yet running the onReg function first.

var tempusername
var temppassword
var tempconpassword

var username
var password

function onReg(tempusername, temppassword, tempconpassword, username, password){

tempusername = document.querySelectorAll("[name=username]")[0].value
temppassword = document.querySelectorAll("[name=password]")[0].value
tempconpassword = document.querySelectorAll("[name=conpassword]")[0].value

if(temppassword == tempconpassword && tempusername.length>2 && temppassword.length>2 && tempconpassword.length>2 ){

    username = tempusername
    password = tempconpassword
    alert(username + " : " + password)

else if (password!=tempconpassword){
    alert("Password doesnt match or is to short!")


function onLogin(username,password) {
    alert("Does this work?" + username)

I think it might be because it's not a Global scope? and if so how could I come across doing it with this code?

username inside onLogin is a formal function parameter. You have to explicitly pass a value to it when calling onLogin.

var x = 'foo';
function checkX() {
function badCheckX(x) {
checkX(); // 'foo'
badCheckX(); // undefined
badCheckX('bar'); // 'bar'
badCheckX(x); // 'foo'  

7 Tips to Handle undefined in JavaScript, In JavaScript, a function that doesn't have return statement implicitly returns undefined : Of course, indicating near return the expression to be returned Helped by ASI, you can remove the semicolons from the previous example: There is one small, but annoying trap created by ASI. My issue occurs when I attempt to use taxRate inside the computeTip function. If I use taxRate, computeTip doesn't understand the value of taxRate, stating it is undefined, and even if I do specify tips::taxRate, before I even compile Visual Studio states. Error: a nonstatic member reference must be relative to a specific object.

I think you should use document.getElementById("[name=username]").value

Upgrading to PHP 5, print "A notice occurredVn"; } $a++; A notice occurred This example sets my error_handler () as the handler for E_NOTICE errors, but lets PHP handle all other Since incrementing an undefined variable triggers a notice, PHP invokes the function. A notice occurred This example is identical to the last one, except that my  When you declare a variable by having a var a statement in a block, but haven't yet assigned a value to it, it is undefined. Again, you shouldn't really ever need to rely on that. Again, you shouldn't really ever need to rely on that.

Do not use variables that have been declared already as parameters of a function. username and password are already declared as variables, so you should replace them with some other names. Try using usernameX and passwordX instead.

function onLogin(usernameX,passwordX) {

    // will alert the value of the first parameter (usernameX)
    alert("Does this work?" + usernameX) 

    //if you want to get the value of the variable 
    alert("Does this work?" + username)


undefined, A variable that has not been assigned a value is of type undefined . A method or statement also returns undefined if the variable that is being evaluated does not have logs "foo string" (function(undefined) { console.log(undefined, typeof so checking the existence of a variable in the global context can be  When a function has no return value;, it returns undefined. There’s generally no need to use such a return result. When you declare a variable by having a var a statement in a block, but haven’t yet assigned a value to it, it is undefined. Again, you shouldn’t really ever need to rely on that.

The 10 Most Common Mistakes JavaScript Developers Make, As a result, the anonymous function being passed to setTimeout() is being If you guess that the console.log() call would either output undefined or throw an error, But we still wouldn't think this would be a problem, since each time through, the scoped num variable) and the element's onclick is set to that inner function. "Undefined function or variable" can be caused by: 1) A typographical error when typing a function or variable name (such as typing a 1 [one] when you intended to type an l [low-case L]). However, later versions of MATLAB try to resolve these typos with “Did you mean” suggestions.

Functions :: Eloquent JavaScript, And the vocabulary that is available tends to be more precisely defined, and thus have a return statement at all, such as makeNoise , similarly return undefined . But bindings created for function parameters or declared inside a function can only functions created new scopes, so old-style bindings, created with the var  Hi everybody, I can't make it work, echo $_POST["eAdresse"] is undefined and I don't know why and how to fix it. Please help me. code page 1:

Variable scope, Every function is meant to output its number. But something is wrong… Why do all of the shooters show the same value? The nested function's [[Enviroment]] is set the moment outer function is in its lexical environment, to be able to use it anytime later: let f = sum(5); // undefined Only when initialization is reached in code (its execution), can they be defined, therefore become accessible. If the let variable is declared and initialized in the same place, the proper value is assigned, otherwise the variable is undefined. The const variable must always be initialized in the same place as it is declared.

  • Where did you call onReg ?
  • can you submit your code on jsfiddle.net so anyone in the community can look into it?
  • In my html <button class="req" onclick="onReg()">Create</button>
  • and what about the variables of onReg(tempusername, temppassword, tempconpassword, username, password)?
  • please share HTML and js else submit your code on jsfiddle.net