JavaScript Test for existence and correct value on an if statement

With JavaScript or CoffeeScript, is there an easy way to test for the following:

if (locals.messages.text === "somestring") { ... }

Now this needs three things to be true: locals needs to exist, locals.messages needs to exist and it needs to equal to "somestring".

If locals.messages doesn't exist, it throws a TypeEror cannot read text of undefined

I know I can do this:

if (!!locals && !!locals.messages && locals.messages.text == "somestring) {...}

but that is too long, is there anyway to:

  1. text for the existance of locals.messages.text
  2. Check if the value is equal to something?

Working in either javascript or coffeescript

Answer:1

I have a webpage aligned properly when viewed fullscreen in a browser, but when the same is resized things go horribly mis-aligned. Here's the html. <html> <head> <style type='text/css'...

I have a webpage aligned properly when viewed fullscreen in a browser, but when the same is resized things go horribly mis-aligned. Here's the html. <html> <head> <style type='text/css'...

function Class () { var self = this; this.hi = true; // this { hi: true }, self { hi: true } self.hi = false; // this { hi: false }, self { hi: false } } Why doesn't self behave like a normal var?

function Class () { var self = this; this.hi = true; // this { hi: true }, self { hi: true } self.hi = false; // this { hi: false }, self { hi: false } } Why doesn't self behave like a normal var?

I have a web form, with text inputs for crontab values (minutes/hours/months/days), and I want to display user the next date of selected crontab task execution. For example, i have a values 20 15 15 *...

I have a web form, with text inputs for crontab values (minutes/hours/months/days), and I want to display user the next date of selected crontab task execution. For example, i have a values 20 15 15 *...

I want to set the selected value of a jQuery Mobile flipswitch: <select id="quote" data-role="flipswitch" data-theme="b"> <option value="nee">Nee</option> <option value="...

I want to set the selected value of a jQuery Mobile flipswitch: <select id="quote" data-role="flipswitch" data-theme="b"> <option value="nee">Nee</option> <option value="...

  1. cannot call methods on dialog prior to initialization
  2. cannot call methods on a stopped sparkcontext
  3. cannot call methods on varchar
  4. cannot call methods on nvarchar
  5. cannot call methods on int
  6. cannot call methods on nvarchar(max)
  7. cannot call methods on date
  8. cannot call methods on button prior to initialization
  9. cannot call methods on autocomplete prior to initialization
  10. cannot call methods on varchar(max)
  11. cannot call methods on bigint
  12. cannot call methods on varchar. sql
  13. cannot call methods on ntext
  14. cannot call methods on float
  15. cannot call methods on tooltip prior to initialization
  16. cannot call methods on tabs prior to initialization
  17. cannot call methods on draggable prior to initialization
  18. cannot call methods on decimal
  19. cannot call methods on sortable prior to initialization
  20. cannot call methods on slider prior to initialization