JavaScript Strict Mode – How Would You Use a Closure Within a Loop?
JavaScript programmers are in high demand by businesses large and small. Consulting firms also are seeking talented, experienced developers to complete business-critical projects on an hourly or contracted basis.
Determining how experienced and knowledgeable a programmer is takes more than a cursory evaluation of a résumé. Probing and analytical questions go a long way toward evaluating a candidate’s skill level.
Functionality common to many JavaScript objects is the use of various types of loops. Most programmers have utilized loop logic in programs, but a more detailed question when conducting an interview would be to query how and when to use closure within a loop.
Why Use Loops at All, and What Types to Use
There are various types of loops utilized in programs, for specific purposes. Experienced programmers should be able to define the types and uses of each form:
- FOR loop – executes a group of code multiple times
- FOR/IN loop – loops depending on the properties of a particular object
- DO/WHILE loop – executes a block of logic while a condition specified is true
- WHILE loop – loops through a block of code while a set condition is true
Loops will ultimately make use of variables in their function to control execution of the logic. Each function of code can create its own variables, which could be defined as local or global variables. Local variables (those created in a function) are not available outside their function, where global variables can be accessed across functions – outside their scope.
A good description of closures is to illustrate how they can be used to ‘hide’ data used in one function from another object or function. By utilizing object oriented programming (OOP) techniques, variables and data utilized within one function can be isolated or encapsulated from other functions.
Loop functions are therefore good uses for closures, where a developer may want a particular function to have access to only certain data or variables, but keep other information private.
This can be particularly useful in web applications and server functions that may be available to many users or even in the public domain, where there is function data that must be protected from access to such objects as public APIs.
JavaScript Strict Mode
Introduced in JavaScript 1.8.5, the ‘use strict’ directive became available to control code execution in JavaScript strict mode. What does that mean to the programmer?
For starters, JavaScript strict mode prevents the use of undeclared variables. When a script includes this declaration at the start, it takes effect for all code contained in the script (global scope). Programmers should also be aware that making the declaration within a function only provides local scope for strict mode.
Why use strict mode?
JavaScript will generate a new global variable anytime a new one is named in a function. In strict mode, this will generate an error, since you cannot create a new variable in strict mode. The benefit is that a mistyped variable name will not create a new one, which can eliminate errors caused by typos.
Programmers can avoid potential coding errors through the use of strict mode:
- Assignment to getter-only or non-writable properties
- Reference to a non-existing property or variable
- Use of an object that does not exist
Since objects are also variables, strict mode disallows the use of any object that has not been declared, and prevents deletion of an object or variable.
Caveats – use strict in JavaScript must be declared at the beginning of a script, to be recognized. There are also strings and keywords that are not available when strict mode has been declared, such as:
- Public
- Private
- Yield
Use strict is supported by IE V10 and higher, Firefox V4 and up, and Chrome since V13.
Understanding Coercion – Does Your Candidate?
Any JavaScript developer must at least be aware of coercion, whether a fan of that characteristic of the language or not. This is actually an attribute of many languages, but that doesn’t make the concept any easier to understand.
Essentially, this coercion typically happens under the covers, but it can take a programmer by surprise, if not expecting the results. Coercion is the property of JavaScript that disallows operations against values of different types. A candidate should understand how coercion reacts when handling numbers and strings or performing Boolean logic.
When comparing values of different types, coercion will take place to transform the values to like types. Developers need to understand this trait of JavaScript, to be prepared for the results. It’s also important to know that values can be compared either with or without coercion through the specification of ‘==’ (allowing coercion to take place) or ‘===’ (no coercion).
Getting the Right Answers
It’s not as important that a JavaScript developer candidate can recite exact syntax for how these functions should be coded. The primary focus should be to determine that the interviewee understands the underlying concepts of JavaScript functions, and a proper application of the techniques – when to use them, and why.