JavaScript JavaScript Loops, Arrays and Objects Tracking Data Using Objects The Student Record Search Challenge Solution

Chuck Comstock
Chuck Comstock
Full Stack JavaScript Techdegree Student 4,238 Points

How to check for a string === null? In this video, Dave writes "Search === null". This doesn't work for me.

I can console.log the prompt value (search) and it will show it equals null. Yet, (search === null) does not work. I also have tried (!search). This also doesn't work. I can verify search does equal null yet I cannot verify it with a conditional statement.

Any help would be greatly appreciated!

Chuck Comstock
Chuck Comstock
Full Stack JavaScript Techdegree Student 4,238 Points

Hi Kris. This is very odd. This code does not work:

do {
    console.log('start');
    var name = prompt("Enter a student name to search (quit to stop)");
    if (name === null) {
        console.log("===null " + name);
    }
    if (name) {
        console.log("name " + name);
    }
    if (!name) {
        console.log("!name " + name);
    }
    if (name != null) {
        console.log("!=null " + name);
    }
    if (name !== null) {
        console.log("!==null " + name);
    }
    console.log('end');
} while (name != null);

I have also used while (name).

The output to the console is strange to me:

start
name null //<- this is strange
!=null null //<- this is strange
!==null null //<- this is strange
end

7 Answers

KRIS NIKOLAISEN
PRO
KRIS NIKOLAISEN
Pro Student 47,519 Points

So I was wrong in that Dave does declare var search; at the top of the code - no matter.

If I change the variable name to search in your original code the result is:

start
===null null
!search null
end

So I looked up name and found it listed here with the following:

JavaScript Objects, Properties, and Methods You should also avoid using the name of JavaScript built-in objects, properties, and methods:

Also found another example on stackoverflow

This doesn't work

name = document.getElementById('nombre');
name.className = 'thinking';

This does work

username = document.getElementById('nombre');
username.className = 'thinking';
KRIS NIKOLAISEN
PRO
KRIS NIKOLAISEN
Pro Student 47,519 Points

Strange. I'll have to look at this further on Monday because I know it worked at my work machine (Mac). I have Windows at home - not sure if that makes a difference. In the video Dave doesn't declare search with var or let. I removed your loop and declared name with let instead of var

    console.log('start');
    let name = prompt("Enter a student name to search (quit to stop)");

    if (name === null) {
        console.log("===null " + name);
    }
    if (name) {
        console.log("name " + name);
    }
    if (!name) {
        console.log("!name " + name);
    }
    if (name != null) {
        console.log("!=null " + name);
    }
    if (name !== null) {
        console.log("!==null " + name);
    }
    console.log('end');

and cancelling the result is:

start
===null null
!name null
end

If I use var or no keyword to declare name the result is:

start
name null
!=null null
!==null null
end
Chuck Comstock
Chuck Comstock
Full Stack JavaScript Techdegree Student 4,238 Points

interesting! I am using Windows 10 and Chrome. What I found is even stranger.

This works:

if (name.toUpperCase() === "NULL") 
Chuck Comstock
Chuck Comstock
Full Stack JavaScript Techdegree Student 4,238 Points

using let in a while (true) works with a break statement. I can't get a Do While loop to work. This is kind of crazy but at least I know how to make it work.

I think the let statement can't work in a Do While because While is outside the {}.

Thank you for your help Kris. I'm curious what you find as well.

Chuck Comstock
Chuck Comstock
Full Stack JavaScript Techdegree Student 4,238 Points

I just read: https://developer.mozilla.org/en-US/docs/Web/API/Window/prompt

"Please note that result is a string. " <- I am assuming this is a weirdness of JavaScript or a bug. null in JS is type Object not type String, which explains why the below code works. It's a little maddening to my brain...

if (name.toUpperCase() === "NULL") 

any thoughts?

thank you again for your help Kris!

Chuck Comstock
Chuck Comstock
Full Stack JavaScript Techdegree Student 4,238 Points

I apologize if I am over posting. This code ends up allowing me to do what I believe is the correct way to check for null. If I use var I get name as a typeOf string. Even if I set it explicitly to null. If I use let. I has to be declared outside of the loop for scope. BUT it does start out as typeOf object (which is what null is).

let equals true null value

let name = null;
do {
    console.log("begin DO");

    console.log(name);
    console.log(typeof (name));

    name = prompt("Enter a student name to search (quit to stop)");

    console.log(name);
    console.log(typeof (name));

    if (name) {
        console.log("name " + name);
    }
    if (!name) {
        console.log("!name " + name);
    }

} while (name && name.toUpperCase() !== "QUIT");
console.log("end DO");
console.log(name);

var equals type of string even when setting to null or not declaring a value

var name = null;
//or var name; //<- same result - typeOf string
do {
   same code here