Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3623972
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:26:59+00:00 2026-05-18T23:26:59+00:00

$.each(json.data,function(i,fb){ //some code here… if (msg.length > 150) { msg = msg.substr(0,200)+…; } //more

  • 0
$.each(json.data,function(i,fb){

//some code here...

if (msg.length > 150) {
                msg = msg.substr(0,200)+"...";
            }

//more code here...
});

I have an issue here. If msg is not defined I got an error and I got nothing displayed on the browser.

Possible Solution: create a default message for the cases where msg is not defined.

Try:

if (msg = 'undefined') {
 msg = 'some default message';
} else if (msg.length > 150) {
msg = msg.substr(0,200)+"...";
}

Issue:
The above try didn’t work because I get for each item, the same default message, instead of having the default message only for those cases were msg is no “set”.

Request:
Can I have your help in order to write the proper code so that, on each item, if msg is not defined show a default one, if otherwise, it’s site, display it ?

If you lack some information in order to proper help me, please, let me know. I’m really on a bad position right now. :s

Thanks a lot,
MEM

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T23:26:59+00:00Added an answer on May 18, 2026 at 11:26 pm

    You have to write == rather than = in your if-statement. Also, if you’re trying to test if it’s undefined, you should use typeof, like this:

    if (typeof msg == 'undefined')
    

    This idea of comparing it using typeof is actually safer than comparing it to the object called undefined. The reason is that the object can be redefined, but the result of typeofnever changes. I dont know why anyone would be crazy enough to do it, but it is possible to write:

    undefined = "hi there";
    if (msg == undefined) // oh noes!
      ...
    if (typeof msg == "undefined") // still works!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my Javascript: $.post('foo.php', { request: Request }, function(data) { $.each(data.chats, function(i, chat)
I'm trying to use the PHP json_encode function to encode some JSON to send
I have a page that display some data. It is loaded from a database
Here is my code. You have to kindly look does it suffer from 'same
Each of my clients can have many todo items and every todo item has
Each item in a WPF ListBox control seems to have a bit of left
I'm trying to increment through JQuery loop to fill out some HTML code with
I have a textbox that for each time a user inputs a letter I
I am using Ext.data.Store to call a PHP script which returns a JSON response
I recently asked a question (and had it answered) here: jQuery Load JSON I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.