Overview

Introduction

Welcome to the Applying JavaScript Best Practices Practice Lab. In this module, you will be provided with the instructions and devices needed to develop your hands-on skills.

Exam Objectives

The following exam objective is covered in this lab:

  • 1.2 Apply JavaScript best practices

Lab Duration

It will take approximately 15 minutes to complete this lab.

Exercise 1 - Applying JavaScript Best Practices

Generally, a programming language has its own style guides, best practices, and coding principles. As a developer, it is a good programming practice to follow the language’s style guides to ensure quality, readability, and maintenance of code.

JavaScript also follows certain best practices and coding conventions such as the use of comments and indentations, naming conventions, and the debugger keyword.

The comments help to add descriptive text to the source code documents that need not be executed. The web browser does not display the text included within the comment symbols. As a developer, you can add single or multiple line comments to add more information about the logic used in the code for better understanding and clarity. You can also add comments to provide a description of the different types of functionality used in the code. You can place comments anywhere in the code.

Indentation is another aspect of writing meticulous code. A neatly indented code helps to identify the program sections such as variable declarations, if-else statement blocks, loop blocks, user-defined functions, output statements, etc. An indented code helps the developers to easily read, add, modify, delete, maintain, update, and understand the entire program effectively. In JavaScript, indentation will not affect the program’s output. It is better to use four spaces to indent the various blocks in the code. You should avoid the usage of tabs while indenting the JavaScript code in editors.

The naming conventions adopted in a programming language help to decide the way you should name the variables and user-defined functions in a program. In JavaScript, a recommended way to name variables and user-defined functions is to use camelCase. In camelCase, two words are joined together and represented as a single word. The first alphabet of the word is in lowercase and the first alphabet of the second joined word is in uppercase. For example, if you declare a variable “resultvalue” in JavaScript, you need to use camelCase, and name it as “resultValue”.

It is very common to have errors or mistakes in the programs. There are different types of errors that can occur in programs. Some of them are run-time, syntax, logical, typing errors, etc. A developer must be capable enough to identify and fix the program errors. One of the best programming practices is to analyze the code, determine the errors, and fix them. This practice is known as debugging. There are built-in debuggers available in all the web browsers. In JavaScript, you can perform debugging using the debugger keyword.

In this exercise, you will learn to apply the best practices followed in JavaScript. You will perform the following tasks:

  • Task 1 - You will download the Visual Studio Code workspace (Calculator) from the Intranet and open the HTML page named Calc.html in the Calculator workspace.
  • Task 2 - You will apply the JavaScript best practices on the Calculator page.

Learning Outcomes

After completing this exercise, you will be able to:

  • Apply the best practices in JavaScript code

Comprehensive Learning

See the full benefits of our immersive learning experience with interactive courses and guided career paths.