Return to JavaScript: Function and Block Scoping
Confession: I’m not organizing my posts in any particular order. It’s really a reflection of what I find interesting as I dig into the “new” world of modern JavaScript.
In my former life as a developer using JavaScript I defined variables with the var keyword. For me, that meant that the some variables were defined at the “root” and were global. Others, defined in a function were limited to the function. A problem could arise in which a global variable is modified in a function leading to unexpected outcomes.
To be fair, most of my work was dealing with doing client side validation of input fields so this wasn’t a frequent problem. My function would accept a value, test it, and the user could “move on” or “try again”.
Now, however, with dynamic pages consisting of a lot of CSS and JavaScript, understanding the management / scope of variables becomes much more important.
Having learned Java, I was familiar with using code blocks to do more than make my close look nice. I could shield variables within blocks as well as functions. Not so with JavaScript var declarations. Enter the let keyword.
“Variables assigned with let are always block scoped. But this isn’t the only benefit to using the let keyword. Variables assigned this way also cannot be hoisted“. This quote, from “Explore New Syntax in JavaScript ES6” is a part of a fantastic primer on the “new” JavaScript universe and I highly recommend it.
Hoisting is a feature of JavaScript that is personally annoying. I grew up in Fortran and still prefer the orderly progression of procedural programming. Hoisting, in case you don’t know, allows a developer to put variable declarations “anywhere” and use them lines (and more lines) before they are declared. Too many times I’ve found myself reading a page, finding a variable and wondering, “Wait, where the heck did that come from?”.
The keyword, let, doesn’t guarantee I’ll know where a variable was declared but, if I’m reading from the beginning, there’s a better chance I’ll know. So, now I have a new tool in my tool box for declaring variables and (very excited) being able to organize them in blocks as well as in functions.
Hooray.
Starting Somewhere: Return to JavaScript
I started down the Salesforce trail a scant two and a half years ago. Today, eager to build useful Lightning Web Components, I’m advised a foundation in JavaScript is essential.
“No problem”, my naive mind declared. I’ve worked with JavaScript for over ten years… just not much in the last eight or so. Meaning, I missed the advent of ES6. Where to start, then? Have no fear, Trailhead to the rescue.
I found some terrific trails relating to the “recovery” effort and, good news for me, I don’t have to start from scratch. With this post, I’m launching a series of posts, “Return to JavaScript” in which I return to a landscape that seems both familiar and alien. I return from when I created my own libraries and toyed with early AJAX using jQuery and Dojo.
The new landscape is filled with wonders and I hope you’ll join me.
Welcome to Another Reboot
Where do I go from here? I ran into a LinkedIn update from a blogger I followed in the once wonderful world of Lotus Notes. We don’t know each other beyond my delight in glutinously consuming his posts. Yet, I feel we have a lot in common. As it happens, each of us moved away from Notes. Each of us has found our way to Salesforce. For me, the beginning of the journey was compulsory. New technologies, corporate numbness, etc. took their toll on the viability of the very nimble platform I came to know and love.
Begrudgingly, at first, and then with enthusiasm I moved into the world of collaborative solutions built on the IBM WebSphere Application Server platform. The enthusiasm I felt was largely derived from my “knack” for understanding complex architectures and working with them to provide solutions to customer challenges. That enthusiasm never quite filled the void left by the slow inexorable retreat of Lotus Notes.
Today? Enter Salesforce. Once again I find myself utterly delighted by a platform that begs to be put to the test of meeting real needs and delivering meaningful solutions. My company, Island Pond Consulting, exists for the simple purpose of helping individuals and organizations solve challenges. I provide assistance through private consultation, mentoring, and training delivery.
How did I get here and where is “here”? The details may appear in another post. But, in a nutshell, I am a Salesforce Certified Administrator and Advanced Administrator. I work with a delightfully named and extraordinary Salesforce Partner, The ReddRaven Group (named for the two co-CEOs Pamela Redd and Raven Slade), to deliver Salesforce Authorized training.
Let the adventure continue!