Javascript | Why does the variable equal undefined?

Related searches

In this code largestGap is equal to undefined when it is logged. Basically this code is turning the string of 1s and 0s into an array and finding the largest gap between two 1s, as you can see looking through largestGap should equal 4, but as stated earlier it returns undefined.

var gaps = [];
var gapCount = 0;
var largestGap = gaps[0];
var string = '10010001000010001001';
string.split('');
var stringArray = Array.from(string);
stringArray.forEach(function(item, array) {
  if (item == '1') {
    if (gapCount > 0) {
      gaps.push(gapCount);
    }
    gapCount = 0;
  } else {
    gapCount++;
  }
});
for (i = 0; i < gaps.length; i++) {
  if (largestGap < gaps[i]) {
    largestGap = arr[i];
  }
}
console.log(`The largest gap in the string is ${largestGap}`);

You initialize largestGap like:

var largestGap = gaps[0];

But the gaps array is empty initially. Initialize it only after the gaps array is populated, otherwise the test later (if (largestGap < gaps[i])) will not work.

There's also no arr variable. Use the gaps variable instead:

var gaps = [];
var gapCount = 0;
var string = '10010001000010001001';
var stringArray = Array.from(string);
stringArray.forEach(function(item, array) {
  if (item == '1') {
    if (gapCount > 0) {
      gaps.push(gapCount);
    }
    gapCount = 0;
  } else {
    gapCount++;
  }
});
var largestGap = 0;
for (i = 0; i < gaps.length; i++) {
  if (largestGap < gaps[i]) {
    largestGap = gaps[i];
  }
}
console.log(`The largest gap in the string is ${largestGap}`);

undefined, How do you check if a value is undefined in JavaScript? The short answer. In modern browsers you can safely compare the variable directly to� If you’re referring to the resulting value of a ‘var’ statement in the console: [code]> var x = 16; < undefined [/code]This is simply because the var statement doesn’t have a value itself - it is a valueless statement.

You are getting value from an empty array thats why first you should fill the gap array the use gap[0] insead of doing it before you even pushed anything to the array

How can I determine if a variable is 'undefined' or 'null'?, undefined. You can also explicitly set a variable to equal undefined: All other values in JavaScript are objects (objects, functions, arrays, etc.). However, if the referenced variable is not even declared anywhere within the scope chain, then a JavaScript exception will occur. One way to avoid this is by declaring all your global variables from the start: var iAmAGlobalVariable; // Holds the value undefined by default If this is not possible, you should use the typeof operator. It does not

you are assigning variables as var gaps = []; var gapCount = 0; var largestGap = gaps[0];

So largestGap in the end also will have no value as it is assigned at the beginning when gaps variable had no value

How to check for undefined in JavaScript, Undefined comes into a picture when any variable is defined already but null to a variable, null is loosely equals to undefined, But here typeof will show object. How to Replace a value if null or undefined in JavaScript? null === undefined // false null == undefined // true great!!! good practice is not to use null at all in js. You cannot not get rid of undefined because it is built in. if you access an undefined variable its === undefinded, but not equals null, right? So to not get confused, just stop using it. define it as undefined, but not as null ;)

JavaScript — Null vs. Undefined. Learn the differences and , Examples Strict equality and undefined. You can use undefined and the strict equality and inequality operators to determine whether a variable has a value. In the following code, the variable x is not initialized, and the if statement evaluates to true.

variable === undefined vs. typeof variable === “undefined” in , In JavaScript, the equal sign (=) is an "assignment" operator, not an "equal to" operator. This is different from algebra. The following does not make sense in algebra:

Definition and Usage. The undefined property indicates that a variable has not been assigned a value, or not declared at all.

Comments
  • largestGap is only defined if largestGap < gaps[i]). But since largestGap starts as undefined, this condition is never true.
  • At the very beginning largestGap is set equal to gaps[0].
  • But gaps[0] is undefined at that point - you only just declared it (as the empty array)
  • Ah I see, I wanted to declare all my variables at the top and didn't realize the error. Thanks for clearing that up! :D