Doesn't output text in javascript

const input = document.getElementById("number")
const btn = document.getElementById("convert-btn")
const output = document.getElementById("output")

btn.addEventListener("click", correctInput)

const correctInput = (num)=>{
if(num === 0){
  output.innerText = "Please enter a valid number"
  
}

}

why is the text not displayed?

the condition is not true, so the text can not be displayed

the fact is that from all the training I have undergone, I don’t know any other answer)

const input = document.getElementById("number")
const btn = document.getElementById("convert-btn")
const output = document.getElementById("output")

btn.addEventListener("click", correctInput)

const correctInput = ()=>{
const number = input.value

if(!number){
   output.textContent = "Please enter a valid number"
  return
}

}

this also not work

this condition doesn’t make sense, if you want to prevent the user from submitting an empty input you can say

if (input.value === "") {
  output.textContent = "Please enter a valid number";
  return;
}
  • you’re if condition is waiting for the condition !input.value to be true, so it can execute the code inside.
  • ( ! )not operator is useful when you want to toggle between two opposite values . (true/false), (1/0) …

happy coding.

You can’t access correctInput before its definition when that function is declared using const or let and function expressions are not hoisted. Move the event listener to after the function definition or define a normal function declaration using the function keyword.


@mohamedelmir An empty string is a falsy value.

1 Like

that’s true, thank you for the correction and the mention

Why does strict equality not work in conditions?

else if(input.value === 1023){
  output.innerText = "MXXIII"
  return
}

Because it is a string. The input element value is always a string.

'42' == 42
true
'42' === 42
false
1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.