When you use the Javascript stringify function, it removes one of the percent signs in the string '%%

Could you explain why the percent sign is not showing up in the output of stringify?

var data = ["dp",'%%'];
var output = JSON.stringify(data);
console.log ('output: ' + output);

This is the current result:

output: ["dp","%"]

Why isn't the output like this instead:

output: ["dp","%%"]

Appreciate your help!

Answer №1

According to the documentation provided for console.log in Node.js, the function follows a printf-like format:

The initial argument is a string that may contain placeholders.
Each placeholder will be substituted with the respective value from its corresponding argument. The supported placeholders include:

%s - String.
%d - Number (both integer and float).
%j - JSON. If circular references are found, it will be replaced by '[Circular]'.
%% - single percent sign ('%'). This placeholder does not require an argument.
In case a placeholder lacks a respective argument, it will remain unchanged.

Hence, whenever %% appears in a string displayed via console.log in Node.js (excluding browsers), it will be swapped out for a single %. Any %s, %d, or %j will be exchanged for a string, number, or JSON string, respectively. Below are some examples:

console.log("This is a string: %s", "Hello, World!");
//= This is a string: Hello, World!

console.log("This is a number: %d", 3.14);
//= This is a number: 3.14

console.log("This is a JSON string: %j", { value: true });
//= This is a JSON string: {"value":true}

console.log("This is a single percentage sign: %%");
//= This is a single percentage sign: %

console.log("If we use %%s and %%d here we get '%s' and '%d'.", "a string", 100);
//= If we use %s and %d here we get 'a string' and '100'.

Using console.log in a browser environment will simply display the plain string without any substitution as mentioned above.

Answer №2

The issue at hand is not related to the utilization of JSON.stringify. I was able to replicate the single % output in node 0.10.36 simply by executing console.log('%%'). This seems to be a problem with the internal usage of util.format within the console.log function in node.

You can find more information on this topic at: https://nodejs.org/docs/latest-v0.10.x/api/util.html#util_util_format_format

On the other hand, when testing in node 4.0, the expected result is returned.

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

Difficulty encountered when assigning object properties as callback results in node-tesseract package

I'm attempting to utilize the outcomes of a callback function as properties of an object. Here is an example of how I am implementing the module I am constructing: var Capture = require("./Capture.js") const example = async function() { let scr ...

The array is producing accurate results, however it is displaying as undefined in the HTML output

I have encountered a problem while working on my project. I am utilizing an API to fetch an array of platforms where a video game is available. Although the array is returning the correct data, I am facing difficulty in displaying these values in my HTML a ...

Eliminate the commas when listing Google Places types

I am currently utilizing the Google Places API and came across these code snippets at https://developers.google.com/maps/documentation/javascript/examples/place-search-pagination. var map, placesList; function initialize() { var pyrmont = new google.ma ...

Decoding JSON string List<string> data in a JsonResult MVC

Having trouble deserializing a JSON string to MyModel and receiving an "Error" from my AJAX call: $.ajax({ type: 'POST', traditional: true, url: '/Employee/Extract', ...

A guide on updating pseudo-class values dynamically using CSS

Imagine a scenario where there is a navigation panel named <div class="nav"> containing 5 anchor links <a>. CSS: .nav a:nth-of-type(1) { text-decoration: underline; } The objective is to switch the underlined element by adjusting the CSS ...

What is the best way to retrieve a JSON array list from the Server?

I have integrated Volley into my Android client to fetch JSON data from a server. The process I followed to obtain the value is: @Override public void onClick(View v) { queue = Volley.newRequestQueue(MapsActivity.this); ...

What is the best way to update the state of the backend (true, false) for toggling in React.js?

I am developing a React.js application to manage household appliances from the front-end side. My main goal is to synchronize the toggle button with the backend state. The sensor device has a property called "armed." I want the toggle button to show ON w ...

Transforming CSV files into JSON format using Pandas

I have a dataset saved in CSV format, where the first row represents column numbers (which can be ignored), the second row starting at Col_4 contains the number of days, and from the third row onward, Col_1 and Col_2 represent coordinates (lon, lat), Col_3 ...

The functionality of displaying an animated gif will not work when submitting a CakePHP ajax form before or after completion

I have exhaustively tested all possible scenarios using prototype, but I am unable to get my busy indicator to show no matter what I do. Despite cross-browser testing, I've had no luck. <?php echo $ajax->submit('Submit', array(' ...

Error: The internal modules of node.js encountered an issue while loading causing an error to be thrown at

After installing Node.js (LTS version) from their website on my Windows system, I created a new directory named "mynode" and placed a text file inside it called "hellonode.js". Upon trying to run node hellonode.js in the command prompt after changing direc ...

Preserve the scroll position while initiating a jQuery dialog

Utilizing Jquery UI Dialog to showcase a popup box In my design, there is a grid on the page where each row contains an icon that triggers a dialog box An issue arises when scrolling down and opening a dialog at the bottom of the grid, as it causes the p ...

In Angular, the exclusion of a name attribute in a textarea element does not automatically generate an instance

I came across this jade template for creating a modal dialog in Angular Material. Unfortunately, I couldn't convert it to HTML because the jade site was not working. md-dialog(aria-label='Reject', ng-cloak='') form(name="rejecti ...

A guide on iterating through an array to extract the initial character from every string

Currently, my focus is on extracting the initial letter of each word in order to create an acronym. I have set up an array where all the capitalized words are stored, and now I need a way to extract those specific characters. To achieve this, I initially ...

Issue: The property 'top' cannot be read as it is null

I'm trying to access the top of my footer, but I keep encountering this error message: Cannot read property 'top' of null Here is the HTML code: <footer class="footer" role="complementary" id="myfooter"> </footer> And in jQuer ...

ReactJS: Encountered an unexpected token "<"

After conducting a thorough search, I couldn't find a solution to the error I encountered while executing the command: npm start The error message I received was: ERROR in ./src/index.js Module build failed (from ./node_modules/babel-loader/l ...

How to pinpoint a particular element in a parsed json using Ruby

I have a JSON string that I need to parse using Ruby. The string looks like this : [ { "id": 1, "player_id": "not_this_one\n", "highscore": 23 }, { "id": 2, "player_id&qu ...

Using JavaScript to redirect browser with a specific string

My goal is to redirect all users who visit a specific URL to another page. While this redirection works effectively, I now need to implement it with a tracking URL. The issue arises when Javascript automatically adds a "/" in front of the tracking string ...

After applying the cellClass in ng-grid, the row border mysteriously disappears

After applying cellClass to color an entire column, the row border disappears. Here is a link to the Plunker demonstration: http://plnkr.co/edit/4IeQQBTIe8sgHVEBUTAp?p=preview. Does anyone have a solution for this issue? ...

Interacting with PHP variables in JSON format

I've recently started using JSON to exchange data between pages, but I am facing a challenge that I can't seem to solve. Essentially, my issue lies in one page where I am utilizing jquery's getJSON method to retrieve JSON data from another ...

The error message "Cannot access 'navigation' property of undefined" indicates that there is an issue with the object being

I am currently working on setting up a basic StackNavigator. Here is a snippet of my code: App.js import React, { Component } from 'react'; import { StackNavigator } from 'react-navigation'; import Login from './src/scre ...