Contact Us

Today, JavaScript is at the core of all modern web applications. Because of its immense reach and relevance in the modern digital space, identifying JavaScript issues, and rectifying the corresponding mistakes are crucial for web developers to understand.

JS-based libraries or frameworks that are used for single-page application development or Server-side renderings are no longer novel.

Using JavaScript for building a web application can be a straightforward task for most developers regardless of their experience. However, the language remains one of the more powerful ones that require a meticulous understanding.

In this blog, we will focus on 9 important issues that arise during Sitecore JSS development and learn how to avoid these in your daily practice. Whether you are a mid-level or senior-level developer working with modern JavaScript (latest ES versions to current date October/November 2021), identifying and rectifying these mistakes will go a long way in shaping the overall quality of your web applications.

 

9 Challenges Faced while Developing with Sitecore JSS

1. The Reassigning of ‘Const’ Variables

This is perhaps one of the most common mistakes that most developers tend to make. There are two ways to declare variables in JS: "const" and "let" (as per ES6). What is important to note is that we are not using "var" in the modern JS codebase.

It is possible to reassign ‘let’ variables but not "const" variables and that stands as the main difference. Below is an example that illustrates this:

sitecore jss development

As you can see, it will return ‘TypeError : Assignment to constant variable’. Hence, it is important to remember while reassigning values to a variable, it is important to use “let”.

Reassigning of Const Variables in sitecore jss


2. Poorly named Variables

Poorly named variables can make any code very difficult to understand. Many inexperienced developers tend to follow patterns like “test”, “test1”, that do not provide any context regarding what these variables intend to do, making it a lot harder to debug the code.


3. Large functions

Another common mistake by many developers is when they try to wrap up all functionalities into one function. Instead of such an approach, it is far more convenient to start coding to break a big function into smaller ones that make them more readable and easier to understand.


4. Inadequate understanding of ‘Double Equals’ and ‘Triple Equals’

This is an important topic for every JS developer. You need to know that ‘double equals’ compare two values without considering their data types. On the other hand, ‘triple equals’ compares two values while considering their data types.

Since the double equals do not consider the data types of the two values, JavaScript secretly casts (convert one data type to another) each value accordingly so that they can be compared. This means that a number and string could be ‘double equal’ but not ‘triple equal’ since they are different data types.

However, it is always best to use ‘Triple Equals’ as the default approach unless there is any special requirement to use the double one.


5. Inadequate Understanding of Asynchronous Code

The asynchronous code in JS is another aspect that can seem challenging to most developers. While it is quite a vast topic, for this blog, we will stick to providing only a small yet common example of how to best understand it for developers in different stages of their coding career/journey.

Inadequate Understanding of Asynchronous Code in sitecore jss

In summary, asynchronous programming is an essential concept in JavaScript that allows your code to run in the background without blocking the execution of other code. Developers can create more efficient and responsive applications by using features like ‘callbacks’, ‘async/await’, and ‘promises’.


6. Using “this” incorrectly

This keyword can be quite confusing in JS. When you are using “this” without a clear understanding of the context, you are likely to not get the desired results, especially when using the ‘callback’ function. 

context understanding in sitecore jss


MicrosoftTeams-image (112)


MicrosoftTeams-image (110)

As we can see in the illustration above, the code returns ‘undefined’ after calling finishMission().

But why?

This is because we are using the ‘setTimeout’ function in the window object context. But there is no mission variable in the same context.

Instead, here is what you need to do:

Not distinguishing between Undefined and Null in sitecore jss

MicrosoftTeams-image (112)

MicrosoftTeams-image (113)


7. Not distinguishing between ‘Undefined’ and ‘Null’

‘Null’ is basically an assignment value given to a variable. The variable which has been assigned as null contains no value.

When we assign null as a value to any variable, it means that it is empty or blank. This is to show that the variable has no value. Also, null is an object in JavaScript.

As the name suggests, undefined means "not defined". In other words, when we declare a variable but do not assign a value to it, the variable becomes undefined.

Unlike null, the value of an undefined variable is set by JavaScript as undefined. The variable gets created at the run-time. When we do not pass an argument for a function parameter, the default value is taken as undefined. Besides, when a function doesn't return a value, it returns as undefined.


8. Not using the ‘strict mode’

Using strict mode should almost be a rule of thumb when it comes to development using JavaScript. This tiny, yet powerful statement at the beginning of the files can go a long way in easing the coding process for a variety of stakeholders. For instance:

‘use strict’ helps remove silent errors.
‘use strict’ makes debugging easier.
‘use strict’ secures your code.
‘use strict’ prevents unexpected assigning.


9. Using Magic values

Using Magic values as the common values in Sitecore JavaScript is a common mistake that we should avoid. Magic value is a constant value that appears in the code context.

Magic numbers are typically unique values, most commonly, numerical.

In JavaScript, using magic numbers can make the code less readable and harder to understand, especially when they are used as magic numbers in various places within the codebase. This can also lead to bugs and errors, as it can be difficult to locate and fix a problem when the cause of the problem is not immediately clear.

Here’s how Magic values should be used:

Using Magic values in sitecore jss

Need Help?

Conclusion

Addressing these errors in the programming approach can not only help build better applications and produce higher-quality outputs but also make code easy to read and understand, thereby contributing significantly to the improvement of the development process as well. This blog has identified some of the most pertinent errors that developers of all stages tend to make when using JavaScript for Sitecore JSS Development.

If you are keen to learn more about these approaches or wish to implement them in your application development process for your business, then reach out to us. Our team of certified Sitecore experts and developers can help you address any problems that you may be encountering in your programming process and implement the right solutions to drive meaningful business growth.

Publications
& Thought Leadership

Publication
Headless Development Using Sitecore JSS (Java Script Services)
Delve into Headless Development enabled by Sitecore Java Script Services. And learn why it is important to design Sitecore JSS into Sitecore JavaScript Services (JSS) designed as Sitecore JSS.
01 (3)
Publication
Creating the First Sitecore JSS Component
This blog details the various steps involved in creating a Sitecore JSS component, a Headless service that enables developers to decouple the data application from the rendering application.
Creating the First Sitecore JSS Component image
EBOOK
The Ultimate Guide to Headless Drupal
With this ebook, find out everything you need to know about making your headless and Drupal decisions.
The Ultimate Guide to Headless Drupal image