Variables
Variables
In order to save the DOM references to your HTML elements, you must store them in variables.
Let’s breakdown the syntax to declare a variable in JavaScript.
const varName = value;
const: A keyword used to declare a variable. JavaScript supports three declaration keywords:var(the original, function-scoped form), andletandconst(block-scoped, introduced in ES6/ES2015). Modern code typically prefersconstby default and usesletwhen reassignment is needed. We will explain in more detail below the difference between these variable types.varName: Name of your variable.=: Similar to most programming languages, the=symbol is used to define and set a variable name to a specific value.;: All JavaScript lines of code and statements must end with a semicolon;.
To define an empty variable, declare it with let and end with a semicolon (e.g let varName;). Note that const declarations must be initialized at the point of declaration.
var, let, and const
In some programming languages, like Java and C++, when declaring a variable you must specify the data type (integer, String, float, boolean, etc. ) In JavaScript, a variable does not need to be specifically defined in its declaration. Instead the keyword var, const, and let define how a variable can be used and/or changed.
var: Variables declared withvarare function-scoped (or global if declared outside any function). They can be reassigned and redeclared within their scope.varis a legacy keyword — modern JavaScript codebases generally preferletandconst.let: Variables declared withletare block-scoped — they are only accessible within the block (typically defined by curly brackets{}) in which they’re declared. Aletvariable’s value can be reassigned, but it cannot be redeclared within the same scope.const: Variables declared withconstare also block-scoped, but their binding cannot be reassigned after they’re initialized. Note that this does not make the value immutable — properties of an object assigned to aconstcan still be changed.
When storing DOM variables, take a moment to think about which type of variable you would use to store DOM references to HTML elements.
You should use the const variable type! We don’t want to be able to redefine or change the variable holding the reference to our HTML elements. This is why we should choose the const variable type.
Let’s look at an example of using the querySelector method.
Example: index.html
<html>
<head>
<title>My Document</title>
</head>
<body>
<h1>Header</h1>
<p>Paragraph</p>
</body>
</html>
Let’s say we want to store a reference to the p element.
- Since there are no attributes attached to this element, no
idorclassname, this eliminates the use of thegetElementByIdmethod. - We can only use the
querySelectororquerySelectorAllmethod. However, since there is only a single instance of apelement, we will elect to use thequerySelectormethod with the element selector associated with its tag namep.
const pElem = document.querySelector('p');
Notice that we use either single or double quotation marks as a parameter to the querySelector method.