JavaScript's equality check (==) failing to validate when two values are the same

In the process of extracting a value from a JSON file, I encounter the following situation:

var sta = req.jsonBody.sta   //where req.jsonBody.sta = open

Based on this code snippet, my variable "sta" should be equal to "open". However, when I execute the following code:

var turnOn = true
var open = "open"
if (sta == open)
{
    turnOn = false
    console.log(sta)
}

console.log(turnOn)

The expected outcome is for the value of turnOn to be false. Surprisingly, the console is printing true instead. I am unsure about where I might have made an error.

Answer №1

Typically, everything seems to be functioning correctly.

req = {jsonBody:{sta:'open'}};

var sta = req.jsonBody.sta;

var turnOn = true;
var open = "open";
console.log('turnOn = '+turnOn);
if (sta == open)
{
    turnOn = false;
    //console.log(sta);
}
console.log('sta = '+sta);
console.log('turnOn = '+turnOn);

Until you encounter incorrect data, such as extra spaces that make it look like open, but in reality contain open.

req = {jsonBody:{sta:'open '}};

var sta = req.jsonBody.sta;

var turnOn = true;
var open = "open";
console.log('turnOn = '+turnOn);
if (sta == open)
{
    turnOn = false;
    //console.log(sta);
}
console.log('sta = '+sta);
console.log('turnOn = '+turnOn);

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

Having issues with custom directives not functioning properly within AngularJS' RouteProvider

Currently, I'm in the process of learning AngularJS and specifically focusing on the route provider feature. I have successfully built a few pages that functioned well independently. Now, my aim is to implement the route provider into my project. In m ...

Having trouble incorporating Duo Web SDK into angular application

We are currently working on incorporating Duo Two-factor authentication into our Angular application. For instructions, you can take a look at the documentation available here. The issue we are encountering is that their JavaScript file searches for an i ...

Tips for making sure a header is consistently at the top of every page during printing

I need help with my website - I have a table that is quite tall and spans across multiple pages when printing. Is there a way to make the header row appear on top of each page when printing? ...

Ways to extract data from a JSON object

When using my web application, the Web API responds with the following JSON object: [ { "templateID":1, "template":"{\r\n \"Body\": \"sample date hete hee. Name\"\r\n}" }, { "templateI ...

"Utilize an HTML file open dialog box to gain access to the server's

Is it feasible to implement a file open dialog box that displays files and directories from the server's file system? In my web application, there is a need for users to select a file that resides on the server. Are there any plugins available for th ...

Executing Function Upon Clicking an Option in Datalist

I'm facing an issue with datalist in my VueJS search input. Why isn't my function getData(), which is triggered by both @click and @keypress.enter, being called when I select an option from the datalist using mouse or enter key? Below is the cod ...

Is there a quick way in ES6 to assign member functions as event listeners?

This question is unique and not a repetition of the query posed in How to access the correct `this` context inside a callback? Although some answers may overlap, the Question at hand differs. The comments below the answers indicate varied solutions, parti ...

Place HTML elements based on the contents of an array

Lines 12-22 My custom function, openManu(), is currently set to display a block element upon loading the page and iterate through an array using a for loop. The purpose of this function is to create an object variable called obj based on the array values ...

Trouble with animating card components in my React app slider

In my React app, I have a slider that moves my card components when any of the icon buttons are clicked. I'm trying to implement a fade animation for each card when it is clicked, but currently, the animation only works when the page is reloaded. Here ...

I am facing constant connection resets while attempting to upload a file to the Tomcat server

Exploring the creation of a website where users can freely post content has been an interesting journey. One challenge I'm currently facing is enabling users to upload files sequentially and send them as a multi-part request in ajax. Unfortunately, I ...

Using pure JavaScript, you can apply a CSS property to hide an element by setting

Is there a way to hide a <section> element in HTML using JavaScript while highlighting the text or showing it in a different way? This is the solution I have tried: document.addEventListener('click', function(){ var selected = windo ...

Adjust the width of a container to exceed 32767 using jquery in the Opera browser

I am looking to create a compact timeline using jQuery, and I want this timeline to have a width exceeding 32767 pixels. Interestingly, when I attempt to modify the width using the jQuery code $(".timelinecontainer").width(32767);, it doesn't seem to ...

Tips for organizing a JSON object based on an integer column such as "3", "2", "5"

I am facing a challenge with sorting a JSON object that has a column containing numbers. The issue arises because the column is of string type. How can I sort string numbers in numerical order? var myArray = [{ name: 'David', total: "6" }, ...

Using jQuery to verify the status of every input when the page loads

My jQuery price calculator is missing a feature to check if inputs are selected when the page loads. Currently, it only recognizes that Apple is checked after clicking on Banana. How can I modify it to automatically check all inputs at the start? I attem ...

Are there any methods available to adjust the size of a View component in react-native?

My react-native application includes a View that contains several components. The layout displays perfectly on iPhone 6 and 5 models, but on an iPhone 4s, the bottom of one component is being clipped slightly. I'm aware of scaling base64 icons, but I ...

Vue.js - encountering an issue with undefined response data

Greetings! I've encountered a script issue while trying to submit a form in VUE. The error message displayed in the developer panel states: "TypeError: error.response.data.errors is undefined". As a newcomer to Vue, I'm seeking some assistance as ...

Change the navbar brand in Bootstrap to be hidden on small screens like mobile devices

I am facing an issue with the navbar on my website where it expands in height when viewed on mobile devices due to not fitting into a single line. I have observed that if I remove the brand, it fits perfectly. However, I want to keep the brand visible on l ...

Using Bootstrap 3's "img-responsive" class can cause images to become distorted when

When an image is rotated 90 degrees inside a modal, the img-responsive feature stops working and the image height exceeds the modal height. function rotateImage(){ var angle = ($('#testImg').data('angle') + 90) || 90; $(&apos ...

An error was thrown: SyntaxError - { was not expected in script.js on line 5 while checking request

I encountered an issue while executing the code snippet below: var req = new XMLHttpRequest(); req.open('GET', 'data.json'); req.onreadystatechange = function() { if ((req.readyState === 4) && (req.status == 200)) { var cus ...

In need of assistance with Ember data! Struggling to deserialize JSON into model

Here is the technology stack I'm currently using: Ember 1.10.0 Ember Data 1.0.0-beta.15 Within my application, I have defined a model as shown below: //models//acceptedtask.js import DS from "ember-data"; export default DS.Model.extend({ userAg ...