1

How can I learn or start learning the right coding style in JavaScript? What are my possibilities and what practical resources can I use to get used to the coding style? Thanks!

var getRequestUserList = function getRequestUserList(req,res,id) {
    User.findOne({_id: id}, function (err, user,next) {
    if (err) {
        console.log(err);
    }
    if(user){
        var userList = [];
        User.find(function (err, users) {
            if (err){
                console.log(err);
            }
            if(users){
                Friend.find({getReq:id,status:0 }, function (err, friends) {
                    if(err){
                        console.log(err);
                    }else{
                        for (var i = 0;i< users.length;i++){
                            for(var j = 0;j<friends.length;j++){
                                if(users[i]._id == friends[j].sendReq){
                                    userList.push({
                                        id : users[i]._id,
                                        name : users[i].name,
                                        surname : users[i].surname,
                                    });
                                }
                            }
                        }
                        res.json({friendUser:userList});
user7637745
  • 965
  • 2
  • 14
  • 27
V.Aleksanyan
  • 577
  • 1
  • 4
  • 7

2 Answers2

1

I recommend to install and use StandardJS. It's an easy to use, hassle-free coding style.

Install it via:

npm install standard --save-dev

Then run through the rules just to quickly get a feel of it.

Finally, create a script in your package.json to run StandardJS, when you need it:

{
  "scripts": {
    "check": "standard"
  }
}

...then you can run it via npm run check


To provide a quick way to yourself to fix most coding style typos, add a fix script to your package.json:

{
  "scripts": {
    "check": "standard",
    "fix": "standard --fix"
  }
}

...and run via npm run fix


To get a more nicer representation of coding style errors, install snazzy via npm install snazzy --save-dev, then modify your package.json like so:

{
  "scripts": {
    "check": "standard --verbose | snazzy",
    "fix": "standard --fix"
  }
}

Just code as much as you can. After a while, you'll get the hang of the coding style of your choice.

Good luck, have fun!

user7637745
  • 965
  • 2
  • 14
  • 27
0

There are code styles like jshint.com or standardJS which also include automatic code formatting.

Rainer Wittmann
  • 7,528
  • 4
  • 20
  • 34