-1

Problem loading this simple java script code

<script type="text/javascript">
var customer={
     name:"Suresh"
     speak:function(){
       return "my name is" +this.name;
     }
     address:{
       street:"188 sector-5 MVP colony",
       city:"Visakhapatnam",
       state: "AP"
     }
};
document.write(customer.speak()+"<br/>");
document.write(customer.name+"lives at"+ customer.address.street+",<br/>");
</script>
progyammer
  • 1,402
  • 3
  • 13
  • 26
Sasi
  • 43
  • 1

2 Answers2

4

Welcome to Stack Overflow. The community is usually glad to help with questions like this, but it is actually better for you to learn how to solve them yourselves. After all, you can hardly post to SO every single time you have some bug.

In this case, what is the best way to track down this problem? It's similar to finding out why your toaster is not working. First, check to make sure it's plugged in. Then, try using a different outlet. Or, try plugging some other device into the same outlet to see if it works. And so on. This is the basic idea called "fault isolation".

You may think you have a problem with document.write (which by the way, as others have mentioned, should be avoided--use console.log instead). To test this, write a separate little program which does nothing more than a single document.write("Hi, Sasi");. Does it work? Then you know document.write is working, and that you are using it correctly.

So now back to the buggy program. The first thing is to simply use all the available tools to help you find any obvious errors. You should be using an editor, or an IDE, which gives you syntax highlighting and may indicate syntax errors--such as with a red squiggly underline. If you are not using an editor or IDE, drop everything else you are doing and find a good one and spend time learning it and setting it up.

Another useful tool is called a "linter", which checks your JS code for common errors and potential problems. Learn what a linter is, and how to use it, and choose one, such as "eslint". To use a linter, you may have to put your JS into a separate JS file, and bring it into your HTML with a tag such as <script src="path/to/my/test.js">. You should be doing that anyway. Do it now.

Next, we have the extremely useful developer console available in the Chrome browser. If you don't know what this is, or have never used it, then again drop everything else you are doing and study its documentation in detail. It has many handy features, but what is relevant in this case is that it will show you syntax (and run-time errors) in your code. For instance, when I cut and pasted your code into the console, it immediately gave me the error

Uncaught SyntaxError: Unexpected identifier

Right away, I know that even before I need to worry about some problem with my logic, or with document.write, I have a syntax error in my program. Furthermore, it will point out the exact line where the error occurs: the line starting with speak:. Why is speak an "Unexpected identifier"? If you cannot figure out the problem on your own at this point, you might adopt the "divide-and-conquer" approach. For example, try removing the entire speak method. But I still get the error, this time on the the address member. Hmmm. maybe there's something above here that's causing the problem on whatever follows. Try removing the name:"Suresh" line above and try again--you will see that the error on speak goes away. So you now know that the problem is somewhere on or in between those two lines. Eventually, you should be able to deduce that you have omitted the comma which is required between key: value pairs in an object literal, which is something that hopefully you knew by reading documentation such as this one, which helpfully says

You simply notate a list of key: value pairs delimited by commas.

Hopefully the above is a good starting point for tracking down at least most of the simple errors in your code, before you run into a more complicated problem that you really need to post to SO about.

1

First of all you object is missing commas, properties of object should be separated using commas. And also as suggestsed by @AndrewLi do not use document.write(). Below is a demo of working code. Refer for disadvantages of using document.write()

var customer={
     name:"Suresh",
     speak:function(){
       return "my name is " + this.name;
     },
     address:{
       street:"188 sector-5 MVP colony",
       city:"Visakhapatnam",
       state: "AP"
     }
};
console.log(customer.speak());
console.log(customer.name+"lives at"+ customer.address.street);
Community
  • 1
  • 1
Manish
  • 4,082
  • 3
  • 28
  • 39
  • There is a "typo" close reason for this kind of situation, which also has the advantage that compared to "answers" it allows the question to be deleted more easily. –  Jan 02 '17 at 07:23