About 460,000 results
Open links in new tab
  1. Set JavaScript variable = null, or leave undefined?

    May 10, 2013 · If a string, than i will set it to an empty string ='', same for object ={} and array=[], integers = 0. using this method saves me the need to check for null / undefined. my function …

  2. JavaScript Check Empty String – Checking Null or Empty in JS

    Jul 5, 2022 · To fix this, we can add an argument that checks if the value's type is a string and skips this check if it is not: let myStr = null; if (typeof myStr === "string" && myStr.trim().length …

  3. When do I initialize variables in JavaScript with null or not at all?

    Apr 29, 2024 · In JavaScript, each variable can hold both object values and primitive values. Therefore, if null means “not an object”, JavaScript also needs an initialization value that …

  4. When do I initialize variables in JavaScript with null or not at all?

    When defining a variable that is meant to later hold an object, it is advisable to initialize the variable to null as opposed to anything else. That way, you can explicitly check for the value …

  5. Replace a value if null or undefined in JavaScript

    This is equivalent to value = value ?? defaultValue. ||= and &&= are similar, links below. This checks if left side is undefined or null, short-circuiting if already defined.

  6. javascript - What's the best way to initialize empty variables in JS ...

    Jul 15, 2013 · but it doesn't really matter if I initialize it as null or undefined: var obj = null; or. var obj = undefined; For strings I personally use: var str = ''; as later on I can do . str += 'some …

  7. Assign empty string to javascript variable - Stack Overflow

    Jul 24, 2015 · var companyString; if(item.companyname !=undefined && item.companyname != null ){ companyString = utils.htmlEncode(item.companyname); } else{ companyString = ''; } …

  8. what is the difference when we initialize a string variable as null

    Apr 11, 2019 · If you are are going to call string methods on s it's better if s is a string. For example, if s is null this is an error: s.substring(0, 0) and you would need to check s before …

  9. javascript null value in string - Stack Overflow

    Apr 6, 2012 · In javascript I have the following: var inf = id + '|' + city ; if id or city are null then inf will be null. Is there any slick way of saying if id or city are null make then blank. I know in ...

  10. 3 Ways to Set a Default Only When null or undefined in JavaScript

    Dec 7, 2021 · To set a default value only in case of a nullish value (i.e. undefined or null), you can do the following: Explicitly Check for Nullish Value. If the left-hand side of an expression is …

  11. Some results have been removed
Refresh