JavaScript - Understanding the varying results of an if/else statement when utilizing a previously declared variable

I have a query regarding the code snippet provided below:

var d = new Date();
var weekday = ["su", "mo", "tu", "we", "th", "fr", "sa"];
var deliver = weekday[d.getDay()];

if(condition){
    if(d.getDay() == 1){
       d.setHours(d.getHours() + 24); // adds a day
    }
    if(d.getUTCMonth() == 0 && d.getUTCDate() == 1){
        // do something
    } else {
        // version 1 or version 2
    }
}

//version 1: var deliver = weekday[d.getDay()];
//           document.getElementById("leverdatum").innerHTML = deliver;

//version 2: document.getElementById("leverdatum").innerHTML = deliver;;

I'm puzzled as to why the output is "tu" when using version 1, and "mo" when using version 2. Can you explain this behavior?

Answer №1

The presence or absence of the keyword var in that specific line has no impact whatsoever.

What truly matters is the statement = weekday[d.getDay()];.

Given that it is currently Monday, executing

d.setHours(d.getHours() + 24); // adds a day
will result in adding one day to the date, making it Tuesday.

However, if you fail to convert the value of d into a day and assign it to deliver after modifying the value of d, the original string will persist.

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

Utilizing PHP and JavaScript to showcase images within a div element

Disclaimer: This message pertains to an academic task. I am seeking clarification on the required steps for completion, rather than direct code solutions. My aim is to enhance my understanding of the fundamental components involved. Currently, I am engage ...

The coverflow of Swiper is not displaying properly within a Div container

Just to clarify, I am not very experienced and do not know many best practices. I am learning as I go along with projects. I am using Swiper for a game list slider, but when I place it inside another Div, it disappears completely. I can position and size ...

Transforming JSON objects, retrieve an empty value in place of undefined

Can someone please offer some advice on the following: I am retrieving JSON data using this code snippet: $.getJSON(jsonPath, function(returnedData){ ... }); The JSON object returned will have a structure similar to this: ... "skin": { "elapsedTextCo ...

Communication between a directive controller and a service via an HTTP call

I'm currently developing an Angular directive that loads a Highchart.js area graph by passing some variables to it. Here's how I am using the directive: <andamento-fondo-area-chart color="#3FAE2A" url="../data.json"></andamento-fondo-a ...

Unable to process despite clicking button (no error messages)

I'm in the process of setting up an options page for my very first extension. As a beginner in coding, I might have made some rookie mistakes along the way. However, I am facing challenges with basic functionality that was previously working seamlessl ...

Issue encountered while adding platform in Cordova version 8.1.2

Attempting to set up a new Cordova project. The version of Cordova being used is 8.1.2 ([email protected]). I ran the cordova create command and it was successful. However, when trying to add the Android platform with the command cordova platform add ...

Using radio buttons to toggle the visibility of a div element within a WordPress website

I am currently working on creating a WordPress page using the custom page tool in the admin interface. My goal is to have 3 radio buttons, with 2 visible and 1 hidden. The hidden button should be automatically checked to display the correct div (although ...

Next.js endeavors to interpret MDX files as basic JavaScript code

Currently, I'm in the process of creating a website using Next.js and incorporating (local) MDX files for my content. However, I've encountered an issue where whenever I add a .MDX file to my source tree and attempt to navigate to it, Next.js thr ...

Looking for assistance in creating an html page with a rotating CSS div containing unordered lists and list items

I am sharing the HTML content of my test page that I am looking to customize similar to some websites I have come across. My goal is to create a rotating div with an unordered list (ul li), where only three divs are displayed at a time while the rest remai ...

Create a string that has been properly formatted

I need a javascript alternative to the StringEscapeUtils in java, specifically for converting input strings like: He didn't say, "Stop!" The desired output format should be: He didn't say, \"Stop!\" Is there a similar function a ...

I am feeling quite lost in trying to figure out how to create a voice mute command using Discord

I've been searching for information on how to create a command that mutes only one specific person I tag in chat, but I'm having trouble finding any guidance on it. As someone new to discord and node js, I could really use some assistance. Mute ...

receive the output of the inquiry

Here's the issue I'm facing: file accounts.controlles.ts import { requestT } from "src/service/request.api"; export const getaccounts = async () => { const response = await requestT({ method: "GET", ur ...

What are the best ways to utilize the Node.js Express compress middleware efficiently?

Rather than relying on widespread availability of white-space-collapse: discard; in CSS, I am seeking to use middleware to trim all whitespace between HTML tags and condense any other whitespace to a single space. Despite being open to a simplistic approac ...

How can you display a specific word instead of the values 'true' or 'false' retrieved from an API in a FlatList component?

I need help figuring out how to render different text based on whether the value fetched from an API is 'true' or 'false'. For example, I want to display 'Hello' if the value is 'true' and 'Good Night' if i ...

Load website once AJAX has finished

Currently, I am using an ajax call to retrieve the color scheme of my website from the database due to having multiple clients with different schemes. My goal is to ensure that the page only loads after the ajax call has completed. Despite an expected dela ...

On the server side, the received Req.body appears as an empty object: { }

import { Injectable } from '@angular/core'; import { Http, XHRBackend, RequestOptions, Request, RequestOptionsArgs, Response, Headers } from '@angular/http'; import { Observable } from 'rxjs/Observable'; impo ...

Is there a way to retrieve real-time information from an API by utilizing the HTTP.Get method?

When working with ANGULAR I am faced with the challenge of pulling data from an API using http.get at regular intervals. The API continuously updates minute by minute traffic data, and I need Angular to display these live updates. Currently, my approach ...

Ways to update a JSON object within an array of objects

I've got an array of objects // Extracted from the database $scope.users = [{"$id":"1","UserID":3,"Name":"A","Selected":false},{"$id":"2","UserID":4,"Name":"B","Selected":false},{"$id":"3","UserID":5,"Name":"C","Selected":false},{"$id":"4","UserID":6 ...

Counting the number of key-value pairs for a specific key in a JSON data can be achieved by implementing

Is there a way to determine if __metadata and ItemToEkbeNav are the root elements for their children who have key-value pairs? I've attempted various methods such as Object.keys().length and Array.isArray(), but haven't been able to retrieve the ...

Positioning a div in the center horizontally may result in content with a large explicit width being

I am in search of a solution to horizontally center content on a webpage, regardless of whether or not it has a defined width. After referencing a Stack Overflow question on centering a div block without the width, I found a great method that works well w ...