Having trouble making the word uppercase in JavaScript

I am currently facing a challenge where I want to convert an entire word into capital letters. However, my approach seems to be encountering some issues. When it comes to individual letters, the toUpperCase method works perfectly fine.

var name = "gates";
for (var i=0; i< name.length; i++){
name[i] = name[i].toUpperCase();
}
name;

Interestingly, when I try using "hello world".toUpperCase(), everything functions as expected. But for some reason, looping through individual characters in an array doesn't yield the desired outcome! Is there a specific property of arrays/strings in JavaScript that I'm missing?

In response to RGraham's point about string immutability preventing modification, I find myself puzzled by the community's negative reaction. The validity of the question is clear to me.

Answer №1

The issue with this code is that trying to access a string using array syntax is strictly for reading. According to the MDN documentation:

When using bracket notation to access characters, attempts to delete or modify these properties will fail. The properties in question are not editable or configurable. (Refer to Object.defineProperty() for further details.)

Therefore, console.log(name[0]) will give you the desired result, but trying to do name[0] = "G"; will not alter the content of the name variable.

Answer №2

Instead of iterating through each letter, you can simply use the following code:

let username = "gates";
username = username.toUpperCase();

Answer №3

In many programming languages, a string is considered immutable which implies that you are unable to modify individual characters or append text without creating a new string.

userName = userName.toUpperCase();

Executing the above code will return the desired result by converting the original string to uppercase and storing it in the variable 'userName'.

Answer №4

As per the information found on this website

let message = "Good afternoon!";
let result = message.toUpperCase();

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

creating a fresh array with elements of a different class category

I am currently working on creating a dynamic array of the derived class in which the base class has a composition relationship with another class. Here are the classes I am using: class Album:public PhotoLab{ public: Album(string); Album(Image* ,string); ...

Bigger than 100x100 Soundcloud profile picture size

Is it possible to retrieve a user's image larger than 100x100 using the Soundcloud API? Upon reviewing their documentation, I have not come across any images exceeding this size: An ideal solution would involve some form of Javascript implementation. ...

PHP array utilized in a dynamic dropdown menu

I am working on creating a PHP array for a select list that has dynamic options populated using JavaScript. My goal is to collect all the options selected and display them on the next page. I was wondering if there is a better way to achieve this task. C ...

Tips for integrating map coordinates into a URL

In my React app, there is a Leaflet map that I want to update the URL dynamically with position information (latitude, longitude, and zoom) whenever the map is moved. For example: app.com/lat,lng,z/myroutes Furthermore, default values for lat, lng, z sho ...

Using Angular.JS to iterate over a nested JSON array using ng-repeat

I am currently working on a People service that utilizes $resource. I make a call to this service from a PeopleCtrl using People.query() in order to retrieve a list of users from a json api. The returned data looks like this: [ { "usr_id" : "1" ...

Transforming arrays with VBA programming

My Excel file contains two worksheets named “Cities” and “Data”. The "Data" sheet has 108264 rows of data, spanning columns up to AT. On the Cities sheet, there is a list of 210 cities from B4 to B214. Column C shows the count of codes used for ea ...

The scope chain in Chrome v71 connects the anchor tag to the inner img tag

Ever since updating to Chrome v71, I've noticed a strange issue with the scope of an anchor tag that contains an img tag inside. Take a look at this snippet: <a href="#none" onclick="debugger;complete();"> <img src="https://clickmeuk.net/w ...

Is it possible for Spring Boot to initiate an action that will dynamically update an image in an HTML document using JavaScript or another method?

I am currently facing a challenge in updating an image on a website built with HTML, while utilizing Spring Boot as the backend technology. As of now, I am using JavaScript to update the image at regular intervals, but the timing does not align with when t ...

At times, Express.js transfers control to the router

While developing a simple CRUD app using Node.js & Express.js, everything seemed to be working fine in my local environment. However, I encountered an issue when deploying it on a real server. Upon clicking the register button, the user registration proce ...

Guide to redirecting data from an external POST request to a customer through a GET request

Within my Express application, I am currently dealing with both incoming POST requests containing a payload from an external source and GET requests sent by my client: router.post('/liveReleaseStore', (req, res) => { let data = req.body.m ...

Choosing options from two separate dropdown menus with the same CSS class name using JavaScript or jQuery: A guide

I am looking to update the values of two dropdowns using the same CSS properties, similar to the design shown in the attached image. Here is the HTML code snippet for reference: <div class="container-fluid" role="main" style="margin-top: 100px;"> ...

Is there a way to stop the OnBlur event from being activated during a freeze in IE11?

I am currently working on an exam application built with React that needs to be compatible with IE11. Within this application, I have implemented an onblur event that triggers a popup alert and increments the user's lockCount in the database when the ...

When attempting to print a 2D array, be cautious of the warning message that may appear indicating there

I'm attempting to display a grid of 8 rows and 5 columns in my output, but whenever I run the code, I encounter the following error message: warning: excess elements in array initializer. Below is the code snippet causing the issue: #include <stdi ...

pure-react-carousel: every slide is in view

Issue I am encountering a problem where the non-active slides in my container are not being hidden properly. This results in all of the slides being visible when only one should be displayed. Additionally, some slides are rendering outside of the designate ...

Dynamically populate 7 select boxes with options using JQuery

I have a webpage that includes 14 different dropdown menus, one for each day of the week (Monday to Sunday). Each day has two dropdowns: one for opening time and one for closing time. I used jQuery to populate all 14 dropdowns with a pre-defined list of ho ...

Changing the value of undefined properties in a JavaScript object

I am struggling to identify and replace null values with 0's in my object. I was able to access the correct member within the loop, but once it exited, the values were no longer assigned as 0. Working with objects in JavaScript is new to me, so I&apos ...

The worth of the text input generated within a while loop

How can I retrieve the value of a text box created in a while loop on the same page using the onchange event in PHP? while($fet=mysql_fetch_assoc($sql1)) { echo '<tr onchange=loadXMLDoc2(this)>'; echo '<td><input ...

Unable to retrieve the URL using the getDownloadURL function from Firebase

I have been using Firebase storage to successfully store images, however I am encountering an issue when trying to retrieve the image URL from a promise. const imageSaveHandler = (e) => { e.preventDefault(); const uploadTask = storage.ref(`i ...

Is there a way to emphasize text within a string of object array items?

I am currently utilizing the data provided below to pass as props in React. The functionality is working smoothly, but I have a specific requirement to only emphasize the words "target audience" within the text property. Is there a feasible way to achieve ...

Restart the _.after function counter

Despite my efforts to search online, I couldn't find a solution for resetting the _.after counter once the code inside has been executed. The goal here is to have the alert box appear only on every 5th click of the button: var cb; cb = _.after(4, fu ...