Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

105
Views
JavaScript won't check for null condition

So, I'm writing a code in JS that asks the user for their name and prints the greeting in the console. If a user clicks 'cancel' that is the value stored in the variable 'name' is null, the code should return "You didn't give a name". Instead, it prints "Hello, null" which is weird.

var name = prompt("what's your name?");

function hello(name) {
  if ((name == null) || (name == "")) {
    console.log('you didn\'t give me a name')
  } else {
    console.log(`Hello, ${name}!`);
  }
}

hello(name);

expected: "you didn't give a name"

result : "Hello, null"

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

var name = null;

function hello(name){
    if ((name == 'null') ||( name == "")){
        console.log('you didn\'t give me a name')
    }
    else {
        console.log(`Hello, ${name}!`);
    }  
}

hello(name);
about 4 years ago · Juan Pablo Isaza Report

0

name is a string here, whose value is "null" and not null, If you fix the check to name == "null", you should get your desired result

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!