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

  • Home
  • SEARCH
  • 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 7086269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:31:44+00:00 2026-05-28T07:31:44+00:00

window.onload = function(){ var obj = ‘{ name : Raj, age : 32, married

  • 0
window.onload = function(){
    var obj = '{
            "name" : "Raj",
            "age"  : 32,
            "married" : false
            }';

    var val = eval('(' + obj + ')');
    alert( "name : " + val.name + "\n" +
           "age  : " + val.age  + "\n" +
           "married : " + val.married );

}

In a code something like this, I am trying to create JSON string just to play around. It’s throwing error, but if I put all the name, age, married in one single line (line 2) it doesn’t. Whats the problem?

  • 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-28T07:31:44+00:00Added an answer on May 28, 2026 at 7:31 am

    Disclaimer: This is not an answer to follow for the best way how to create JSON in JavaScript itself. This answer mostly tackles the question of "what is the problem?" or WHY the code above does not work – which is a wrong string concatenation attempt in JavaScript and does not tackle why String concatenation is a very bad way of creating a JSON String in the first place.

    See here for best way to create JSON: https://stackoverflow.com/a/13488998/1127761

    Read this answer to understand why the code sample above does not work.

    Javascript doesn’t handle Strings over multiple lines.

    You will need to concatenate those:

    var obj = '{'
           +'"name" : "Raj",'
           +'"age"  : 32,'
           +'"married" : false'
           +'}';
    

    You can also use template literals in ES6 and above: (See here for the documentation)

    var obj = `{
               "name" : "Raj",
               "age" : 32,
               "married" : false,
               }`;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a js code: window.onload = function() { document.getElementById(Button1).onclick = function() { var
My CKEditor code is window.onload = function() { var editor = CKEDITOR.replace( \'big_info\' );
How to rewrite the code using jQuery? <script type=text/javascript> window.onload = function(){ var array
I have this function in my head: <head> window.onload = function(){ var x =
window.onload = function(){ var outerFn = function ( oParam ){ return oParam; } var
Can someone explain why the alert returns undefined instead of hello? window.onload = function()
Why does this occur: window.onload = function(){ var bob=new Number(1); for (var i=0; i<8
When I try the following: <html> <body> <script type=text/javascript> window.onload = function() { var
Here's my code, it's based off the example provided at MDN: window.onload = function(){
This is my script : window.onload = function (){ var title = document.getElementsByTagName('h1')[0].id =

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.