-2
if(typeof    (document.getElementById("courseId").value!=="undefined") || document.getElementById("courseId").value!==null)
    {
        Courseid = document.getElementById("courseId").value;
    } 
MrCode
  • 63,975
  • 10
  • 90
  • 112
Matrix
  • 83
  • 1
  • 1
  • 7
  • 1
    What is `#courseId`? An ``? – Eric Jun 20 '13 at 07:42
  • 1
    http://stackoverflow.com/questions/801032/why-is-null-an-object-and-whats-the-difference-compared-to-undefined?rq=1 – Marvin Emil Brach Jun 20 '13 at 07:43
  • 1
    OP is clearly a new user of SO and perhaps was not used to finding duplicate questions. It looks like a very reasonable question to me. Were all those downvotes actually necessary? – port5432 Oct 30 '15 at 10:32
  • I don't think this question is a duplicate since it's clearly asking about the type of a value in an input. – Shayan Mar 03 '19 at 15:30

3 Answers3

9

rewrite it that way:

if(document.getElementById("courseId") && document.getElementById("courseId").value)
{
    CourseId = document.getElementById("courseId").value;
} 
Marco Forberg
  • 2,634
  • 5
  • 22
  • 33
2

If you explicitly want to check for undefined and null you can do

if(document.getElementById('courseId') === null || 
   document.getElementById('courseId') === undefined) {
    //logic
}
glosrob
  • 6,631
  • 4
  • 43
  • 73
0

An input's value will never be null or undefined - it will be the empty string, "".

Eric
  • 95,302
  • 53
  • 242
  • 374
  • `document.getElementById("myInput").value.typeof` returns `undefined` for me. Even when I assign a value to it by typing in the input box like typing `20` it still returns `undefined`. – Shayan Mar 03 '19 at 15:22