JavaScript Variables

Variable declaration

Variables in JavaScript can be declared in two ways. By using the keywords of “var” or “let”.

var syntax:

var monkey;

Let syntax:

let vendingMachine;

Var is the older way to declare a variable, while let is a newer way. It is recommended to utilize let in modern JavaScript programming, but you will still run into var in various programs. It is important to understand what it does.

Why is let considered to be better than var? The answer is in scope and encapsulation. We will get into scope and how it is used in JavaScript later, but a rudimentary look is prudent here. When the var keyword is used, depending on how and where it is used, the variable and its contents could be available to parts of the program that should not be accessing it. If var is used to declare a variable inside a function, it is available to that function. If it is declared outside the function, it is global. Var is considered to be function-scoped.

By using let, the variable is scoped properly to protect any adverse effects occurring in your program. Let limits access to the variable within the block where it is defined. In JavaScript, a block is defined by curly braces {}. Let is block-scoped.

There is a third way to define a container in JavaScript…const. The const keyword stands for constant. Const is also block-scoped. The difference between const and let is that const variable values cannot be reassigned. Note that they can be mutated. NEED PAGE discussing mutability

Const syntax:

const dingo;

We will cover scoping (function and block) later. The bottom-line is that you should use “let” and “const” as much as possible.

Note: Multiple variables can be declared in a single statement. Instead of…

let oneLet;
let twoLet;
let threeLet;

The following can be written…

let oneLet, twoLet, threeLet;

Happy Coding!

Clay Hess

More To Explore

Code

Exploring the Broadcast Channel API: Inter-Tab Communication

Intercommunication between different contexts (like tabs, iframes or workers) of the same origin has often been a challenge. With the Broadcast Channel API, developers now have a powerful tool to easily communicate between browsing contexts. In this blog post, we’ll dive deep into the capabilities of the Broadcast Channel API, exploring its features, use cases, and how it can be effectively implemented in your projects.

computer, laptop, work place-2982270.jpg
Code

Unlocking Wireless Communication: A Dive into the Bluetooth API

Wireless communication has become an integral part of our daily lives, and Bluetooth technology is at the forefront of this revolution, enabling devices to exchange data over short distances and creating a world more interconnected than ever before. At the heart of this technology lies the Bluetooth Application Programming Interface (API), a powerful tool for developers looking to harness the capabilities of Bluetooth in their applications. In this blog post, we’ll explore what the Bluetooth API is, how it works, and the possibilities it opens up for innovation in wireless communication.

Share This Post

small_c_popup.png

Need help?

Let's have a chat...


Login

Jump Back In!

Here at Webolution Designs, we love to learn. This includes sharing things we have learned with you. 

Register

Begin Your Learning Journey Today!

Come back inside to continue your learning journey.