{"id":461,"hash":"bb8ac8b0f5f7d18204282f5d8f2a620faff36df579682bb33e8a84fedd87949c","pattern":"Is there a way to check for both `null` and `undefined`?","full_message":"Since TypeScript is strongly-typed, simply using if () {} to check for null and undefined doesn't sound right. \n\nDoes TypeScript have any dedicated function or syntax sugar for this?","ecosystem":"npm","package_name":"typescript","package_version":null,"solution":"Using a juggling-check, you can test both null and undefined in one hit:\n\nif (x == null) {\n\nIf you use a strict-check, it will only be true for values set to null and won't evaluate as true for undefined variables:\n\nif (x === null) {\n\nYou can try this with various values using this example:\n\nvar a: number;\nvar b: number = null;\n\nfunction check(x, name) {\n    if (x == null) {\n        console.log(name + ' == null');\n    }\n\n    if (x === null) {\n        console.log(name + ' === null');\n    }\n\n    if (typeof x === 'undefined') {\n        console.log(name + ' is undefined');\n    }\n}\n\ncheck(a, 'a');\ncheck(b, 'b');\n\nOutput\n\n  \"a == null\" \n\n  \n  \"a is undefined\"\n\n  \n  \"b == null\"\n\n  \n  \"b === null\"","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/28975896/is-there-a-way-to-check-for-both-null-and-undefined","votes":781,"created_at":"2026-04-19T04:51:11.496884+00:00","updated_at":"2026-04-19T04:51:11.496884+00:00"}