Lanka Developers Community

    Lanka Developers

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Shop

    Top 7 Javascript Best Practices For Beginners 👍

    Front-End Development
    javascript front-end beginner programming web development
    12
    23
    4897
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • b6
      b6 last edited by b6

      alt text


      1. Always use camelcase when declare variables.


      ❌ Don't Use

      var first_name = document.ElementById("firstname").value;
      var date_of_birth = document.ElementById("dob").value;
      

      ✔ Use

      var firstName = document.ElementById("firstname").value;
      var dateOfBirth = document.ElementById("dob").value;
      

      2. Use shorten IF in small conditions.


      ❌ Don't Use

      //Un-Compressed Code
      if ( age < 19 ) {
            isMajor = false;
      } else {
            isMajor = true;
      }
      

      ✔ Use

      //Compressed Code
      isMajor = ( age < 19 ) ? false : true; 
      

      3. Declare Variables outside of loop.


      Not in all cases. Do it when you use common components/functions/DOMs inside the loop.

      ❌ Don't Use

      for(var i = 0; i < someArray.length; i++) {
         var container = document.getElementById('container');
         container.innerHtml += 'my number: ' + i;
      }
      
      /* 
      This will load the DOM each time of the loop. 
      Will slowdown your application performance in big cases.
      */
      

      ✔ Use

      var container = document.getElementById('container');
      
      for(var i = 0; i < someArray.length; i++) {
         container.innerHtml += 'my number: ' + i;
         console.log(i);
      }
      

      4. Reduce the single global variables.


      ❌ Don't Use

      var userId = '123456';
      var userName = 'username';
      var dateOfBirth = '06-11-1998';
      
      function doSomething(userId,userName,dateOfBirth){
            //Your Implementation
      }
      

      ✔ Use

      //Make a collection of data
      var userData = {
         userId : '123456',
         userName : 'username',
         dateOfBirth : '06-11-1998'
      }
      
      function doSomething(userData){
            //Your Implementation
      }
      
      function doSomethingElse(userData.userId){
            //Your Implementation
      }
      

      5. Always comment your works & function input,outputs.


      ❌ Don't Use

      function addNumbers(num1, num2){
           return num1 + num2;
      }
      
      function getDataById(id){
           return someDataArray[id];
      }
      

      ✔ Use

      /*
      * Adding two numbers
      * @para num1 (int), num2 (int)
      * @return int
      */
      function addNumbers(num1, num2){
           return num1 + num2;
      }
      
      /*
      * Adding two numbers
      * @para id (int)
      * @return Array
      */
      function getDataById(id){
           return someDataArray[id];
      }
      

      6. Always Build/Minify your JS before move it to live.


      Commenting your works, New lines & other best practices will increase your javascript file size. We need this stuffs in Development Environment only.

      So Build & Minify your javascript file using build tools. It will compress your javascript to load much faster.

      Asserts Build Tool : Webpack
      Online Javascript Minifier : javascript-minifier


      7. Load your scripts just before </body> Tags.


      ❌ Don't Use

      <head>
           <!--Don't Load JS files here.-->
      </head>
      
      <body>
            <!--Don't Load JS files here.-->
      

      ✔ Use

                <!--Load JS  files here.-->
           </body>
      </html>
      
      1 Reply Last reply Reply Quote 10
      • Nubelle
        Nubelle Web Development last edited by

        Thanks Bro

        b6 1 Reply Last reply Reply Quote 0
        • root
          root Linux Help last edited by

          Nice post bro.

          b6 1 Reply Last reply Reply Quote 0
          • b6
            b6 @Nubelle last edited by

            @Malith 👌👌

            1 Reply Last reply Reply Quote 0
            • b6
              b6 @root last edited by

              @root thx bro

              1 Reply Last reply Reply Quote 0
              • devR
                devR last edited by

                useful post @b6

                b6 1 Reply Last reply Reply Quote 0
                • GeethOnion
                  GeethOnion last edited by

                  Very useful 😍

                  b6 1 Reply Last reply Reply Quote 0
                  • dev_lak
                    dev_lak last edited by

                    Nice post bro

                    b6 1 Reply Last reply Reply Quote 0
                    • b6
                      b6 @devR last edited by

                      @devR Thx bro

                      1 Reply Last reply Reply Quote 0
                      • b6
                        b6 @GeethOnion last edited by

                        @GeethOnion Thx bro

                        1 Reply Last reply Reply Quote 1
                        • b6
                          b6 @dev_lak last edited by

                          @dev_lak thx bro

                          1 Reply Last reply Reply Quote 0
                          • tnlthanzeel
                            tnlthanzeel Web Development last edited by tnlthanzeel

                            isnt it better to load the javascripts just before closing the body tag? so the page will render faster

                            b6 1 Reply Last reply Reply Quote 0
                            • b6
                              b6 @tnlthanzeel last edited by b6

                              @tnlthanzeel yep.. the assets will.be loaded quickly before javascript. So the site renders before, functions loads after

                              tnlthanzeel 1 Reply Last reply Reply Quote 0
                              • tnlthanzeel
                                tnlthanzeel Web Development @b6 last edited by

                                @b6 niceee

                                1 Reply Last reply Reply Quote 0
                                • lkdev
                                  lkdev last edited by

                                  wow, nice looking post with very useful content, thanks @b6

                                  b6 1 Reply Last reply Reply Quote 0
                                  • b6
                                    b6 @lkdev last edited by

                                    @lkdev 😊😊😊

                                    1 Reply Last reply Reply Quote 0
                                    • DevKasun
                                      DevKasun last edited by

                                      useful article

                                      b6 1 Reply Last reply Reply Quote 0
                                      • dee_kas
                                        dee_kas Web Development last edited by dee_kas

                                        Nice work.really useful

                                        b6 1 Reply Last reply Reply Quote 0
                                        • kapilsri
                                          kapilsri last edited by

                                          really good thing

                                          b6 1 Reply Last reply Reply Quote 0
                                          • b6
                                            b6 @DevKasun last edited by

                                            @DevKasun thx bro

                                            1 Reply Last reply Reply Quote 0
                                            • 1
                                            • 2
                                            • 1 / 2
                                            • First post
                                              Last post

                                            0
                                            Online

                                            3.7k
                                            Users

                                            1.3k
                                            Topics

                                            5.3k
                                            Posts

                                            • Privacy
                                            • Terms & Conditions
                                            • Donate

                                            © Copyrights and All right reserved Lanka Developers Community

                                            Powered by Axis Technologies (PVT) Ltd

                                            Made with in Sri Lanka

                                            | |