Welcome back. In the previous lesson, you ran JavaScript with Node.js, used console.log, and learned to read runtime errors rather than treating them as mysteries. Now we give those scripts useful information to work with.
In this lesson, you will declare variables with let and const, choose clear names, and represent common values with JavaScript’s primitive data types. This is a small but important step toward blockchain development: a block height, wallet address, transaction hash, token amount, and confirmation status are all values—and the way you represent them affects correctness.
Plan for roughly 45 minutes: a short video, two focused readings, and a Node.js script you will run yourself.
Variables: names attached to values
A program often needs to remember a value so it can use it later. A variable gives that value a name.
let blockHeight = 210000;
console.log(blockHeight);
Here:
letdeclares a variable.blockHeightis the variable name.210000is its current value.=assigns the value to the name.
When Node runs the file, it looks up blockHeight and prints:
210000
You can declare a variable first and assign a value later:
let transactionHash;
transactionHash = "0xabc123";
But when you already know the initial value, put declaration and assignment together. It is easier to read:
let transactionHash = "0xabc123";
A variable declared with let can be reassigned:
let remainingConfirmations = 3;
remainingConfirmations = 2;
remainingConfirmations = 1;
console.log(remainingConfirmations);
A confirmation count is a reasonable use of let: its value is expected to change as additional blocks arrive.
Watch this brief overview before continuing. It reinforces the distinction between declaring a name, assigning a value, and updating it.
JavaScript Variables and Datatypes in 6 Minutes
Watch “JavaScript Variables and Datatypes in 6 Minutes” by Learndev for a compact visual introduction to variables, reassignment, and the most common primitive types.
Watch the variable idea for the labeled-container model. Then watch declaration and assignment and let and const. Continue with numbers, strings, booleans, and empty values. Focus on what each value represents, not merely the syntax.
Choosing between const and let
Modern JavaScript gives you two declarations to use almost all the time:
- Use
constwhen you will not reassign the variable. - Use
letwhen you expect to assign a different value later.
const networkName = "sepolia";
let confirmations = 0;
confirmations = 1;
console.log(networkName);
console.log(confirmations);
networkName remains attached to "sepolia" throughout this small script, so const communicates that intention. confirmations changes, so it needs let.
Trying to reassign a const produces an error:
const networkName = "sepolia";
networkName = "mainnet";
Node reports an error similar to:
TypeError: Assignment to constant variable.
A useful beginner rule is:
Start with
const. Change it toletonly when the value genuinely needs reassignment.
This prevents accidental changes and makes a script easier to inspect. const does not mean that the value is universally true forever; it means that this variable name will not be assigned a different value in this part of your program.
Also note that const requires a value immediately:
const chainId = 1;
This is invalid because nothing is assigned:
const chainId;
Names should explain the data
Use camelCase for ordinary variable names: start with a lowercase word, then capitalize each later word.
const walletAddress = "0x1234";
let latestBlockHeight = 210000;
let isFinalized = false;
Names should answer, “What is stored here?” Compare:
let x = 210000;
with:
let latestBlockHeight = 210000;
The second version makes the program understandable without additional detective work. Avoid vague labels such as data, thing, or value when a specific name is available.
JavaScript names can contain letters, digits, _, and $, but cannot start with a digit. They also cannot use language keywords such as let, const, return, or function.
For values that are fixed, known before the program begins, teams often use uppercase names with underscores:
const ETHEREUM_MAINNET_CHAIN_ID = 1;
Use that uppercase style sparingly—primarily for hard-coded, widely meaningful constants. A value that simply happens not to change during one script should generally stay in camelCase:
const selectedChainId = 11155111;
Read the following portions of JavaScript.info for the language rules and naming conventions behind these habits.
Read “Variables” from JavaScript.info to consolidate declaration, reassignment, constants, and naming practices. Its examples use browser alert, but the same declarations work in your Node.js files with console.log.
In “A variable,” read declaration and assignment, paying attention to the recommendation of one declaration per line. Then read the full “Variable naming” section, including the naming rules. Finish the “Constants” and “Name things right” sections. Focus especially on why uppercase names are reserved for hard-coded constants and why descriptive names reduce debugging effort.
Primitive types: what kind of value is this?
A variable name is not permanently tied to one type in JavaScript. JavaScript is dynamically typed: the value currently stored determines its type.
let result = 42; // number
result = "confirmed"; // string
JavaScript permits this, but it is usually clearer to avoid changing a variable’s meaning halfway through a program. A variable called blockHeight should hold a numeric block height, not later become an error message.
The most useful primitive types for the work ahead are shown in the JavaScript data types diagram.

number: ordinary numeric values
JavaScript uses one number type for whole numbers and decimals.
const blockHeight = 210000;
const gasPriceGwei = 24.5;
Use number for ordinary counts, measurements, percentages, and calculations. A block height or a gas limit can usually be represented as a number.
However, JavaScript number values cannot precisely represent every very large integer. The largest integer that is reliably exact is:
For blockchain software, that limitation matters. Token balances and amounts in base units such as wei can easily exceed that range.
bigint: exact large integers
A bigint stores an integer of arbitrary size. Write n after an integer literal:
const amountWei = 1250000000000000000n;
That represents Ether in wei, where one Ether corresponds to wei.
Use bigint when exact integer precision matters: token amounts, wei balances, and cryptographic quantities are common future examples. A bigint cannot include a decimal point, and you should not casually mix number and bigint in arithmetic.
const feeWei = 21000000000n;
const extraWei = 5000000000n;
console.log(feeWei + extraWei);
Both values are bigint, so the result stays exact.
string: text and identifiers
A string is text surrounded by quotes.
const networkName = "sepolia";
const walletAddress = "0x1234...abcd";
const transactionHash = "0x9f8e...c7d6";
Wallet addresses and transaction hashes may look like large hexadecimal numbers, but they are usually handled as strings. They are identifiers, not quantities you should add or subtract.
JavaScript accepts single quotes or double quotes:
const chainName = "Ethereum";
const testNetwork = 'Sepolia';
Backticks are also useful when you want to insert a value into text:
const confirmations = 2;
console.log(`Confirmations: ${confirmations}`);
boolean: a yes-or-no state
A boolean can only be true or false.
let isFinalized = false;
let isWalletConnected = true;
Do not put quotes around boolean values:
const isConfirmed = true;
This is a boolean:
const isConfirmed = "true";
This is a string containing four letters. They look similar when printed, but JavaScript treats them differently. In the next lesson, booleans will control conditional decisions.
undefined and null: two different kinds of absence
undefined means that JavaScript has no assigned value yet:
let transactionHash;
console.log(transactionHash); // undefined
null is an intentional choice to say “there is currently no value”:
let rpcError = null;
For example, rpcError = null can mean the program has deliberately recorded that no RPC error is present. Later, it may become a string if an error occurs:
rpcError = "Connection timed out";
A practical distinction:
- Use
undefinedfor a value that has not been assigned. - Use
nullwhen your program deliberately represents “none,” “empty,” or “unknown.”
JavaScript also has the primitive type symbol, used for unique identifiers. It is real and occasionally useful, but it is not needed for the blockchain scripts you are building now.
Checking a type with typeof
Use typeof to inspect the type of a value:
console.log(typeof 42); // "number"
console.log(typeof 42n); // "bigint"
console.log(typeof "Ethereum"); // "string"
console.log(typeof true); // "boolean"
One historical JavaScript quirk is important to memorize:
console.log(typeof null); // "object"
Despite this result, null is not an object. Treat this as a long-standing language mistake rather than a rule you need to reason from.
Use the data-types reading to reinforce the selection guide, especially the large-integer limit and the distinction between null and undefined.
Read “Data types” from JavaScript.info for a more precise explanation of JavaScript primitive values. This is particularly valuable for blockchain work because it explains when number loses exact integer precision and when bigint is appropriate.
Read the sections “Number” and “BigInt” in sequence. In the BigInt discussion, follow the precision explanation and connect it to representing wei exactly. Then read “String,” “Boolean (logical type),” “The null value,” and “The undefined value.” In the boolean and null discussion, read boolean and null examples. Finally, read “The typeof operator,” including the null quirk.
Build and run a blockchain-flavored value snapshot
Create a file named variables.js in the folder you used last lesson. This script does not connect to Ethereum. It simply models the kinds of values a future blockchain script may hold.
const SEPOLIA_CHAIN_ID = 11155111;
const recipientAddress = "0x1234...abcd";
const amountWei = 1250000000000000000n;
let confirmations = 0;
let isFinalized = false;
let transactionHash;
let rpcError = null;
confirmations = 1;
console.log("Network chain ID:", SEPOLIA_CHAIN_ID, typeof SEPOLIA_CHAIN_ID);
console.log("Recipient:", recipientAddress, typeof recipientAddress);
console.log("Amount in wei:", amountWei, typeof amountWei);
console.log("Confirmations:", confirmations, typeof confirmations);
console.log("Finalized:", isFinalized, typeof isFinalized);
console.log("Transaction hash:", transactionHash, typeof transactionHash);
console.log("RPC error:", rpcError, typeof rpcError);
Save it and run:
node variables.js
Your output will resemble:
Network chain ID: 11155111 number
Recipient: 0x1234...abcd string
Amount in wei: 1250000000000000000n bigint
Confirmations: 1 number
Finalized: false boolean
Transaction hash: undefined undefined
RPC error: null object
Notice the last line carefully: rpcError contains null, but typeof reports "object". That is the historical quirk from the reading, not evidence that your variable contains an actual object.
As a short hands-on extension, make two safe edits:
- Change
confirmationsto2, rerun the file, and confirm thatletsupports reassignment. - Replace
rpcError = nullwith a string such as"Request timed out", rerun, and observe that its type changes to"string".
Do not try to update SEPOLIA_CHAIN_ID unless you deliberately want to inspect the const reassignment error. It represents a fixed hard-coded protocol value in this script.
A compact selection guide
When deciding how to represent a value, ask two questions:
-
Will this variable be reassigned?
If no, start withconst; if yes, uselet. -
What does the value mean?
Choose its primitive type according to its role, not according to what it visually resembles.
| Value you want to represent | Typical declaration | Why |
|---|---|---|
| A fixed named network | const networkName = "sepolia"; |
Text that will not be reassigned |
| A changing confirmation count | let confirmations = 0; |
Ordinary number that changes |
| A token amount in wei | const amountWei = 500000000000000000n; |
Exact integer; use bigint |
| A wallet address or transaction hash | const address = "0x..."; |
Identifier stored as text |
| A status flag | let isFinalized = false; |
Yes-or-no state |
| An intentionally absent value | let rpcError = null; |
Explicit “no error/value” state |
| A not-yet-provided value | let transactionHash; |
JavaScript leaves it undefined |
You can now declare values with const and let, use meaningful camelCase names, and choose among the primitive types that appear constantly in JavaScript and blockchain tooling. In particular, remember that addresses and hashes are strings, booleans are unquoted true or false, and large on-chain integer amounts often require bigint rather than number.
Next, you will use boolean values in a more active way: comparison, equality, and logical operators will let your program make decisions with conditional logic.