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

developer writing code at his laptop with code surrounding him in multicolored smoke
Code

Exploring the CSS Properties and Values API

The CSS Properties and Values API is an exciting part of the CSS Houdini suite of APIs that enables developers to define and register custom CSS properties directly in JavaScript. This API introduces advanced capabilities like type checking, default values, and control over whether custom properties inherit their values. These features significantly enhance the power and flexibility of CSS in modern web development.

Developer sitting outdoors at a coffer shop working on his laptop with colors swirling
Code

Exploring the CSS Paint API: Unlocking Creativity in Web Design

The web is constantly evolving, and with it, the tools available to developers and designers expand. One of the most exciting additions to modern web design is the CSS Paint API (also known as Houdini’s Paint API). This feature allows developers to create dynamic, programmatically generated images directly in CSS without the need for external assets or heavy graphical libraries.

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.