Why isn't my JavaScript code displaying any results when using the document.write() function?

Having trouble with this simple JavaScript code

<script type="text/javascript">
var user={
     name:"John"
     speak:function(){
       return "Hello, my name is" +this.name;
     }
     location:{
       street:"123 Main Street",
       city:"Anytown",
       state: "CA"
     }
};
document.write(user.speak()+"<br/>");
document.write(user.name+"lives at"+ user.location.street+",<br/>");
</script>

Answer №1

Greetings, users of Stack Overflow. Our community is pleased to offer assistance with inquiries like these, yet it is highly beneficial for individuals to cultivate their problem-solving skills. It's not practical to rely solely on SO for every bug encountered.

When faced with a problematic scenario such as this one, it's akin to troubleshooting a malfunctioning toaster. Begin by confirming the power source, then try an alternate outlet or test another device in the same socket. This method, known as "fault isolation," aids in pinpointing issues.

Your concern may involve the usage of document.write (a practice to avoid—consider employing console.log instead). To assess this, create a simple program featuring just document.write("Hi, Sasi");. Does it execute without errors? If so, you're utilizing document.write correctly.

To address the faulty code, utilize available tools to detect any glaring mistakes. Utilize an editor or IDE providing syntax highlighting and error notifications. Consider using a linter like "eslint" for error checking in your JavaScript code. Familiarize yourself with the developer console in Chrome to identify code errors swiftly.

If encountering syntax issues, adopt a "divide-and-conquer" strategy by removing segments of code to isolate the error. Refer to documentation for guidance on proper syntax usage within object literals, ensuring correct punctuation between key-value pairs.

The aforementioned strategies should aid in resolving basic coding errors before resorting to seeking assistance on online forums for more complex issues.

Answer №2

Your object is missing commas to separate the properties, kindly ensure to include them. Another important point raised by @AndrewLi is to avoid using document.write(). You can refer to this link for more information on why it is considered bad practice: here

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);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Adjusting the content of a single text box by typing in another

Is it feasible to automatically convert a Nepali date input in one textbox into an English date and display it in another textbox without any page refresh? I have a PHP function that can translate dates between Nepali and English, and I want it to execute ...

What is the best way to upgrade to a specific version of a child dependency within a module?

npm version: 7.24.2 Looking for assistance on updating a child dependency. The dependency in question is: vue-tel-input This dependency relies on libphonenumber-js with version ^1.9.6 I am aiming to update libphonenumber-js to version ^1.10.12. I have ...

I am looking to initiate the animation by triggering the keyframe upon clicking a button

Is there a way to create an animation triggered by a button click instead of loading the page? Each table data cell has its own class with a unique keyframe defining the style changes over time. @-webkit-keyframes fadeIn { from { opacity:0; ...

`Issues with AJAX PHP file upload`

I've been working on an AJAX PHP upload script, but I'm facing some issues. Once the user selects an image to upload, it should display in the specific div container specified in my javascript file (which is working fine). However, I believe ther ...

The ForbiddenError has struck again, this time wreaking havoc in the realms of Node.js, Express.js

I am currently adapting this GitHub sample application to utilize Express instead of KOA. However, I am encountering an Access Denied issue when the / route in Express attempts to load the index.html. What specific modifications are required in the code be ...

"Troubleshooting: Why isn't my jQuery AJAX POST request successfully sending data to

Here is the jQuery code snippet that I am currently working with: $("#dropbin").droppable( { accept: '#dragme', hoverClass: "drag-enter", drop: function(event) { var noteid = "<?=isset($_POST['noteid']) ? ...

Javascript code requires server to have default text field values

Based on the choice made by a user, a text field box will either remain concealed or revealed: $("#aForm").on("change", function() { if ($(this).val() == "a") $("#textField").hide(); else $("#textField").show(); }); The issue arises when the ...

Within Vuex, the object store.state.activities contains a specific key labeled "list" which initially holds an array of three items. However, when attempting to access store.state.activities.list directly, an empty array

My project is utilizing Vue. The store.state.activities object contains 2 keys, including an array named list with 3 elements. However, despite attempting to access it using store.state.activities.list, I am getting an empty array. I have experimented w ...

The price filter slider is experiencing issues with the onresize function not functioning properly

I am facing an issue with a price filter I developed for my project. Despite having coded it, the filter is not functioning properly. <div class="price_range_caption"> <span class="currency_from">Rs.</span><span id="price_range_f ...

Interacting with my Rails API through JavaScript requests

Exploring the world of Rails and diving into creating a basic rails-api. Currently facing an issue while trying to incorporate user addition to my model using a JavaScript request... Let's take a look at my HTML file named add-user.html: <script ...

Issue with Submit Button Functionality in Django Form Submission

I'm currently facing an issue with an HTML template I built using Bootstrap. My problem lies in the fact that I have a JavaScript function that dynamically adds rows to a Django form with specific fields whenever I need to. However, when I add a row, ...

What steps should I take to ensure that the getElementsbyName function works properly on both Internet Explorer and Firefox browsers

Encountering a JavaScript error in IE but not in FF ("document.getelementsbyname(...).0.innerhtml is null or not an object": var oldVal = parseInt(document.getElementsByName("outSL")[0].innerHTML); //value pulled from the database Here is the asp.net c ...

Is there a way for me to modify the formatting of the text that I

Can you help me transform a text that looks like this: '?category=1,2&brand=1' into something like this: '?categories[]=1&categories[]=2&brands[]=1'? I'm not quite sure how to make this change. ...

Is there a way to calculate the height of a component that is only rendered conditionally?

I have a scenario where I need to dynamically adjust the height of a component that is conditionally rendered without explicitly setting it. The height should automatically match the size of its content. To achieve this, I am using a reference to determin ...

Web Browser cross-origin AJAX requests

Recently, I developed an Internet Explorer extension that injects a script every time a page is accessed. The purpose of this script is to send an AJAX request to my localhost. However, I have encountered a roadblock where the request only works if it&apos ...

Complete and send HTML forms with changing IDs

I am facing an issue with storing data from two HTML forms with dynamic ID attributes using AJAX calls. Currently, the AJAX call only works for one HTML form with a static ID name "surveyImage". I am unsure how to individually call the submit() method for ...

Transforming a class component into a functional component using React for a session

While working on a React application, I encountered the need for auto logout functionality for inactive users. After referring to this resource, I attempted to convert my class component code to functional components. However, I faced issues as the functio ...

What is the optimal location for making API requests in a React/Redux/Reach Router application?

I'm struggling to figure out the optimal location for making API calls in my app. My main component structure looks like this: ReactDOM.render( <React.StrictMode> <Provider store={store}> <Router /> ...

Dealing with submit errors in React Redux forms

I am facing a challenge with handling exceptions properly when submitting in redux forms. I want to display a toast message when an error occurs. LogIn.js: class LogIn extends PureComponent { onSubmit = (values) => { const { login } = this.props; ...

Is there a way to modify the variable in order to switch the font of the heading every second using CSS and JavaScript?

I'm trying to create a heading that changes fonts every second, but I'm having trouble changing the variable to set it to another font. Check out my code here. Despite watching tutorials, everything seemed too confusing. var root = document.q ...