Variables, Declarations and Assignment Flashcards
What is the **<script>**
tag, what are its two key attributes, and how does webpack use it?
The <script>
tag allows us to inject JavaScript code into the browser either directly in the tag or by defining a src="example/file-location.js"
.
Traditionally a type="text/javascript"
attribute was needed to tell the browser what type of code to expect, but this is now inferred thanks to HTML5 and is unecessary.
Because JavaScript is run top-to-bottom it is important to place the <script>
tag just before the closing </body>
tag to ensure the document has been fully loaded by the browser before running any code.
If using Webpack, webpack will automatically generate a bundle of all the project JS code called main.bundle.js
, and add a <script>
tag to your HTML with src="main.bundle.js"
.
What is the reserved keyword var
used for?
varis a JavaScript keyword for creating unique identifiers which store mutable values.
Why is JavaScript considered a dynamically typed language?
JavaScript is considered a dynamically typed language because variable types are mutable, or dynamic.
Ex: A variable that was initialized as a number can become a string.
What is varibale decleration?
Variable decleration is when a unique identifier is declared using either var
, let
, or const
.
What are the three JavaScript keywords for declaring variables?
The three JavaScript keywords for declaring variables are var
, let
, and const
.
What is variable initialization?
Variable initialization is when a variable is given a value with the =
operator.
Ex: const a = 10;
- variable a is declared & initialized to number 10.
What is variable hoisting?
Hoisting refers to the process that the JavaScript engine uses to bring declared variable names to the top of the script automatically.
What is the let
statement in JavaScript?
let
is a keyword in JavaScript used for declaring variables. The main difference between let
and var
is in regard to scoping of the variables.
Whereas var
is function scoped, let
is block scoped. This means that variables declared with var
are accesible in the global scope, whereas variables declared with let
are only available inside the block they are declared in. let
is also not accesinle before initialzation, wherease var
is.
Use let
if you will need to rebind a variable.
What is the const
statement in JavaScript?
const
is a JavaScript keyword used for declaring variables which cannot be re-bound, i.e. they cannot change value once initialized.
Best practice is to use const
by default.