0% found this document useful (0 votes)
2 views2 pages

Understanding JavaScript Variables and Types

Uploaded by

alutaconscious17
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Understanding JavaScript Variables and Types

Uploaded by

alutaconscious17
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

- Variables

1. In your own words, explain the purpose of a variable.


Ans: A variable is a labelled container for storage of data in JavaScript for later use in the
code. With the variable, data regardless of its type will not have to be repeated and can be
reused in subsequent codes.

2. Declaration Keywords: What are the three keywords you can use to declare a
variable in JavaScript? For each one, explain its key characteristics regarding
scope and whether it can be reassigned or re-declared.
Ans: The 3 keywords for variable declaration are var, let and const.

Var - This is a flexible method of declaring variables as it can be reassigned and


redeclared in the code, e.g.:

var name = "Favour" // Declared


name = "Chiburuoma" // Reassigned
var name = "Amaewhule" // Redeclared
Let - This is a less flexible method of declaring variables. When declared in a function, it is
only available within the function and cannot be redeclared within that scope but can be
reassigned, e.g.:

let age = 16 // Declared


age = 18 // Reassigned
let age = 17 // Cannot be redeclared
Const - This method is not flexible in many instances. It cannot be reassigned or
redeclared, e.g.:

const food = "Soup"

3. Naming Rules: List three specific rules for naming a variable in JavaScript. Then,
provide an example of a valid variable name and an invalid one, and explain why the
invalid one is not allowed.
Rules for variable naming:

- Names must start with a letter, an underscore or a dollar sign.


- Variable names are case sensitive so Name and name are different.
- Reserved keywords in JavaScript such as let, const, if etc cannot be used as variable
names.

var preferredSport = "Football" // Correct


var 1student = "Bob" // Incorrect
This is incorrect because variable names cannot begin with a number.

- Data Types
4. Dynamic Typing: What does it mean for JavaScript to be a 'dynamically' or
'loosely typed' language?
Ans: This means that variable data types do not need to be stated beforehand. During
declaration, JavaScript is able to discern the data type.

5. Primitive Types: For each of the five classic primitive data types listed below,
provide a brief definition and a code example of a variable being assigned a value of
that type.
• String - This refers to text and cannot be used in calculation later in the code.

var animal = "cat"


• Number - This consists of numerical values used in calculation.

let age = 24
• Boolean - This represents either true or false.

var isLoggedIn = true


• Null - stands for variables left empty usually intentionally.

let gamerId = null


• Undefined - every variable declared but without a value.

var breakfast;

6. What is the fundamental difference between null and undefined in JavaScript?


Ans: For the data type null, the variable has been declared to contain the word null.
However, the undefined data type has no assignment at all and ends with a semicolon (;)
instead of possessing an equals to sign (=).

7. Type Identification: Look at the values below. For each one, identify its data type.
• "Hello World" - String
• 42 - Number
• true - Boolean
• "false" - String
• null - Null
• ["apple", "banana", "cherry"] - Object

You might also like