0

Undefined Problem

Hey guys... In javascript how to say if the variable is undefined then do nothing or print nothing? I tried the line below but it didn't work! If ( typeof txt === 'undefined' ) { Txt= null; } It still prints the undefined!!! Please help!!

19th Aug 2020, 11:58 AM
Shaghayegh
Shaghayegh - avatar
15 Answers
+ 4
var a; if a==undefined do something
19th Aug 2020, 12:02 PM
Abhay
Abhay - avatar
+ 3
if (!txt) { //do something }
19th Aug 2020, 12:29 PM
Chiku
Chiku - avatar
+ 1
Shaghayegh Hp , you have used three === and put a string, 'undefined'. Either put 2 equal tos or remove the quotes.
19th Aug 2020, 12:32 PM
Arnesh
Arnesh - avatar
+ 1
Arnesh thanks it worked
19th Aug 2020, 6:57 PM
Shaghayegh
Shaghayegh - avatar
+ 1
x==null try this 👶 with the if condition
20th Aug 2020, 4:08 AM
Sanjay Kamath
Sanjay Kamath - avatar
0
Arnesh here is my code, it still dosent work. and prints"undefined" https://code.sololearn.com/WUE0oR3HX6Z3/?ref=app
19th Aug 2020, 1:20 PM
Shaghayegh
Shaghayegh - avatar
0
Shaghayegh Hp , remove the typeof
19th Aug 2020, 1:57 PM
Arnesh
Arnesh - avatar
0
Arnesh it still dosent work!! 🤦‍♀️
19th Aug 2020, 2:34 PM
Shaghayegh
Shaghayegh - avatar
0
typeof work with () Ex- Console.log(typeof(5)); //number
19th Aug 2020, 2:54 PM
Divya Mohan
Divya Mohan - avatar
0
Divya Mohan it still dosent work, maybe the other part of my code has a problem, did you check it?
19th Aug 2020, 2:57 PM
Shaghayegh
Shaghayegh - avatar
0
What do you want as result that not executing here.
19th Aug 2020, 3:15 PM
Divya Mohan
Divya Mohan - avatar
0
Divya Mohan how i can avoid this code from printing "undefined" after adding five comment?
19th Aug 2020, 3:23 PM
Shaghayegh
Shaghayegh - avatar
19th Aug 2020, 5:14 PM
Arnesh
Arnesh - avatar
0
How could be the typeof txt be undefined. It will be always a string that's inside promt box or no comment . Then why are you making a condition for typeof(txt)=== 'undefined' what do you want after clicking button 2
19th Aug 2020, 5:46 PM
Divya Mohan
Divya Mohan - avatar
0
I am not a javascript person but I think it is also a case sensitive language for which you have to be sure that the Txt!=txt. Moreover, I respect your question but, you could check it by printing in the if the condition to get sure about the condition. Hope you understand what I mean. And after correction please mention that if there is == or === in javascript as I believe there must be two equals and the text has to be in a string like "undefined".
20th Aug 2020, 12:30 AM
Sufian Ahmad
Sufian Ahmad - avatar