Can someone explain me why we used curly braces "{}" in this code?

what are curly brackets used for
curly brackets in math
when to use square brackets
what are curly braces used for in javascript
types of brackets
what are curly brackets used for in python
can you use brackets in an essay
brackets symbol

While I was trying to check how many times an element used in an array, I found this code. It is written by another user and I got it to work but I am trying to figure out why he used "{}" at the end. I know that .reduce() method can get initialValue but I could not understand the use of braces.

var a = ["a","b","b","c","a","b","d"];  
 
 var map = a.reduce(function(obj, b) { obj[b] = ++obj[b] || 1;    
 return obj;
  }, {});

The braces {} represent a new empty object in javascript, In your case, it will be the object returned by the reduce method to the map variable, we need to initialize it first then fill it in the core of the reduce callback.

Value to use as the first argument to the first call of the callback. If no initial value is supplied, the first element in the array will be used. Calling reduce() on an empty array without an initial value is an error.

It's the initialValue take a look to reduce(), here's a sample, If you were to provide an initial value as the second argument to reduce(), the result would look like this:

let arr = [0, 1, 2, 3, 4];

arr = arr.reduce((accumulator, currentValue, currentIndex, array) => {
  return accumulator + currentValue;
}, 10);

console.log(arr);

Can someone explain me why we used curly braces, The braces {} represent a new empty object in javascript, In your case, it will be the object returned by the reduce method to the map variable, we need to� If you are asking can they be used with formulas other than IF, absolutely, hundreds of functions support them. If this answer solves your problem, please check Mark as Answered. If this answer helps, please click the Vote as Helpful button.

But when I remove the braces and run it, I get: a

This is the syntax:

arr.reduce(callback[, initialValue]) 

initialValue : Value to use as the first argument to the first call of the callback. If no initial value is supplied, the first element in the array will be used."

So, if you use reduce without the initialValue({}), the first item in the array will be used as the initialValue which is "a"

So, it becomes similar to:

var a = ["a", "b", "b", "c", "a", "b", "d"];

var map = a.slice(1).reduce(function(obj, b) {
  obj[b] = ++obj[b] || 1;
  return obj;
}, "a");

console.log(map)

Can anyone explain to me why curly brackets can be optional when , Can anyone explain to me why curly brackets can be optional when running a I have a general understanding of how linked lists work, but I've never used one� So we can use .reduce() to process different data types and get different result like in the example. I have seen the use with mathematical operators like reducing an array of number to a single number etc. By using this method I can .reduce() some data and assign it to an object. Thanks for quick reply! – Oğuzhan Alpagut Demiryürek Mar 29

That is the accumulator object.You can say that it is the initial value so when the call back function will be executed the initial value will be a empty object.

So in the example below initially it is passing an object which have key e

var a = ["a", "b", "b", "c", "a", "b", "d"];

var map = a.reduce(function(obj, b) {
  console.log(obj)
  obj[b] = ++obj[b] || 1;
  return obj;
}, {e:'test'});

console.log(map)

Are curly braces ever used in normal text? If not, why were they , They're occasionally used in 'normal' writing when other brackets and braces Note they include an example in the definition, I've taken a larger Thomas Blount's entry for brace in Glossographia (1656) (you can see on When I edit a web page on Wikipedia, in the Edit summary (Briefly describe the� It probably also has something to do with the fact that curly braces are used in set notation in mathematics, making them somewhat awkward to use for array element access, rather than things like declaring "set"-ish things like structs, arrays, etc. Even modern languages like Python use curly braces to declare sets and dictionaries.

The second argument in the .reduce() method is the initialValue, which is a

Value to use as the first argument to the first call of the callback. If no initial value is supplied, the first element in the array will be used.


tl;dr

It's the initial value which .reduce() starts with. This is the first argument passed to the callback in the first call.


In your case the idea was to build a map of values from the array where keys in the map were the values from the array and values in the map were a number of occurrences of that value in the array.

A map in JS can be easily simulated by an object which in your case has been passed as a literal {} to the .reduce() method. The method fires the callback for each element in the array passing the result object as the first argument and the current element in the array as the second. But the problem is at the first call - what value should be used as the result object if there were no previous elements in the array to accumulate? That's why you need to pass some initial value to have something to start with. As the MDN states, if no initialValue is passed, the first element of the array is used - that's why you got a when removed initial value. When you passed [] you told JS to have an array literal as the initial value but in the callback, you treat it as an object which is allowed in JS since an array is also an object. The problem arises when you try to iterate over those properties or stringify them using JSON.stringify(). But it's for another story ;)

Square Brackets, Curly Brackets, Angle Brackets, Oh My!, Let me give you folks a glimpse of life at Scribendi.com. When I was assigned the article on brackets, I was overjoyed (okay, not really). contains the word "it," the author of the paper will frequently use brackets to clarify the antecedent. So we can omit curly braces only there is a single statement under if-else or loop. Sometimes for debugging purpose we just make a line commented. Using this we check the effect of output without that statement. In that time the if-else or loops without braces makes some troubles. For example −

{} create new object if you don't add this then the first element in the array will be used.

You can see that when you run the code with {} you get an empty object as the initialValue and fulfills your requirement.

var a = ["a","b","b","c","a","b","d"];  
 
var map = a.reduce(function(obj, b) { 
    "use strict";

    if (Object.entries(obj).length === 0 && obj.constructor === Object) {
        console.log("InitialValue is defined as object: ", obj);
    }
    obj[b] = ++obj[b] || 1;    
    return obj;
}, {});

console.log(map);

Grammar: Braces Usage, Using braces in grammar is simple because this type of punctuation is only required for About Us � Contact Us � Suggestion Box � Privacy Policy � Terms of Use Examples of how to use braces in writing can help you understand their purpose. Adding italics to a quotation: "I have found the best way to give advice to your� Additionally, an optional argument allows me to adjust the kerning between letter and index, i.e. W_{\ind[-2mu]{x}}. This works quite well. But surprisingly, I have to put braces around the \ind-command in order to make it run. Can someone explain me why I have to put braces around the command and how I can modify the macro to avoid this?

Help w/ semicolons and curly brackets!, Please use our new forums at discuss.codecademy.com. 0 points. Submitted by I don't understand where and when you put semicolons and curly brackets. I thought I did, and Could someone explain it to me? This is what I ..also if the text is not EXACTLY like it is in the instructions there will be errors. correct: "You' re� Using Visual Studio, I just started coding a little fill in the blank type story. Nothing sophisticated. It's not finished but please explain "do I need curly braces or not", why, and why when executed if you answer y to a boy, it ask the name and then gets stuck vs answering n to a boy, it ask the girls name and continues.

Please explain the different sintaxes for "Object Literal Notation , Can someone kindly share his/her knowledge and time to tell me why we have the We would not use any form of brace or bracket in these instances. Otherwise, you will need to use a curly brace. The good news, aside from example #2, it's always better to err on the side of using the brace. But here are some common examples of when you need to use curly braces. Use curly braces where you: render your JSX element render() { return goes here } initialize state state = { name: "jeff" }

Bracket, A bracket is either of two tall fore- or back-facing punctuation marks commonly used to isolate a brackets (US), square brackets, closed brackets, hard brackets, third brackets, They can be used in various fields as notation to indicate the amount of is often used to indicate omitted material: "I'd like to thank [several�

Comments
  • Documentation: Array.prototype.reduce() syntax. arr.reduce(callback[, initialValue]) "initialValue : Value to use as the first argument to the first call of the callback. If no initial value is supplied, the first element in the array will be used."
  • I know that .reduce() method can get initialValue but I could not understand the use of braces. well, you can understand it if it's var obj = {}, this is the same - notation for an object literal. So your initial value would be an empty object.
  • "I thought that they might be the initialValue parameter" - yes it is. "but when I tried to remove the braces the result was not the same" - why did you expect the same result when you changed the call?
  • Thank you all for commenting, I did not know that the {} meant an empty object. So I was curious if it had some special function (being an object in this case) or it was just to sort the content.
  • Thank you, I get it. I just started learning about objects, so I did not know what "{}" meant. So when we put {} it means, take these values, process them, and assign them to the variable as an object, right?
  • When we do {} we initialize the accumulator obj with an empty object like obj={};, then inside the callback of reduce where's filling this object with the processes data.
  • Thank you for clarifying. It really helped me. I am just starting to learn about objects so I am looking forward to test this tip on my own. Thanks!
  • You're welcome, glad I could help, Happy coding. (Congratulations, you've earned the VoteUp privilege consider to use it if any answer helps you).
  • I see. So I know, .reduce() gets an accumulatorValue and processes it with the currentValue but how does it work in "obj[b] = ++obj[b] || 1;" part? İnitialValue is an empty object. That means reduce will start with an empty object. Means {}[b] = ++{}[b] I guess. But what is the "b" value, I am still trying to figure out. :)
  • So we can use .reduce() to process different data types and get different result like in the example. I have seen the use with mathematical operators like reducing an array of number to a single number etc. By using this method I can .reduce() some data and assign it to an object. Thanks for quick reply!
  • Thank you for explaining. :) It seems a bir advanced for me, but I understood what you meant.