JavaScript Variables and Data Types for Beginners
New to code? Learn what JavaScript variables and data types are, with simple copy-paste examples using let, const, and var explained step by step.
New to code? Learn what JavaScript variables and data types are, with simple copy-paste examples using let, const, and var explained step by step.
If you have ever typed "javascript variables explained for beginners" into a search bar, you are in the right place. This guide answers it in plain words: a variable is a labeled box that stores a piece of information (like a name, a price, or a true/false answer) so your program can use it later. By the end of this article, you will know how to create variables with let, const, and var, and how to recognize the basic data types JavaScript uses to store information.
No prior coding experience is needed. Every term is explained the first time it appears, and every code example is short enough to copy, paste, and run yourself.
A variable is a named container that holds a value in your computer's memory. Instead of typing the same piece of data over and over, you give it a name once and reuse that name anywhere in your code.
let userName = "Maria";
console.log(userName);Line 1 creates a variable called userName and stores the text "Maria" inside it, using the let keyword. Line 2 uses console.log(), a built-in function that prints a value to the browser or terminal console, so you can see what userName currently holds.
JavaScript gives you three keywords for creating variables. They all do the same basic job (store a value under a name), but they behave differently when it comes to changing that value later and where in your code they are visible.
| Keyword | Can you change the value? | When to use it |
|---|---|---|
| let | Yes, you can reassign it | For values that will change, like a counter or a form input |
| const | No, it stays fixed after creation | For values that should never change, like a fixed tax rate |
| var | Yes, but with older, confusing rules | Avoid in new code; you will mostly see it in old tutorials |
let score = 10;
score = 20; // this works, because "let" allows reassignment
const pi = 3.14;
pi = 3.15; // this throws an error, because "const" cannot be reassignedscore is declared with let, so giving it a new value on line 2 works fine. pi is declared with const, so trying to change it on line 5 causes JavaScript to stop and show a TypeError. As a simple rule for beginners: start with const for every variable, and only switch to let if you know the value needs to change.
JavaScript is strict about how you can name a variable. Follow these rules or your code will not run:
A data type describes what kind of value a variable is holding, like text or a number. JavaScript checks this automatically, so you never have to declare the type yourself. The most common types beginners meet first are called primitive types, meaning they hold a single, simple value.
let city = "London";
let greeting = 'Hello there';A string is any text wrapped in quotes, either double quotes (") or single quotes ('). Both lines above create strings; JavaScript treats double and single quotes the same way.
let age = 28;
let price = 19.99;Unlike some languages, JavaScript uses a single number type for both whole numbers (28) and decimals (19.99). You never wrap a number in quotes; if you do, it becomes a string instead.
let isLoggedIn = true;
let hasDiscount = false;A boolean can only ever be one of two values: true or false. Booleans are what your code checks inside conditions, like "if the user is logged in, show their profile."
let middleName;
console.log(middleName); // undefined
let promoCode = null;
console.log(promoCode); // nullmiddleName is declared but never given a value, so JavaScript automatically sets it to undefined. promoCode is deliberately set to null, which is a value programmers use to mean "empty on purpose." The difference is intent: undefined usually means a value was forgotten, while null means a value was intentionally left empty.
Strings, numbers, and booleans hold one value each. When you need to group several values together, JavaScript gives you arrays and objects.
let fruits = ["apple", "banana", "cherry"];
let user = { name: "Maria", age: 28 };fruits is an array, a list of values in a specific order, written with square brackets [ ]. user is an object, a collection of named properties written with curly braces { }, where name and age are labels for their values. Arrays and objects are big enough topics for their own guide, so for now just recognize the square-bracket and curly-brace shapes when you see them.
If you are ever unsure what type a variable holds, JavaScript has a built-in tool called typeof that tells you.
console.log(typeof "hello"); // "string"
console.log(typeof 42); // "number"
console.log(typeof true); // "boolean"
console.log(typeof undefined); // "undefined"Placing typeof before any value or variable returns a string naming its type. This is a handy debugging habit when your code is not behaving the way you expect.
Once you are comfortable creating variables, the next beginner skill worth learning is how databases store and retrieve data using SQL.
Read the SQL beginner's guidelet and const were introduced in 2015 and are scoped to the block of code they appear in, while var is the older keyword with looser, more confusing rules. Use const by default, switch to let when a value needs to change, and avoid var in new code.
No. JavaScript is a dynamically typed language, meaning it figures out the type automatically based on the value you assign, such as text, a number, or true/false. You can check the type at any time using the typeof operator.
No, once a const variable is assigned a value, it cannot be reassigned to a different value. Attempting to do so causes JavaScript to throw a TypeError, which is why const is the safest default choice for values that should not change.
undefined means a variable has been declared but has not been given a value yet, which JavaScript sets automatically. null means a developer deliberately set a variable to represent "no value," so it signals intent rather than an oversight.
The core primitive types are string (text), number (whole numbers and decimals), boolean (true or false), undefined, and null, plus symbol and bigint for more advanced cases. Beyond these, arrays and objects let you group multiple values together.
Want help building a real web application with clean, beginner-friendly code behind the scenes?
Talk to our teamOccasional, no-fluff notes on shipping modern software — startups, automation, Laravel, Shopify and more. No spam, unsubscribe anytime.
Keep reading
What is an index in SQL? A plain-English guide with copy-paste examples showing how indexes speed up queries and when you actually need one.
Learn JavaScript DOM manipulation for beginners: select elements, change text and styles, and handle clicks with simple, copy-paste code examples.
Let's build something
Book a free discovery call. We'll listen, ask sharp questions, and send you a proposal within 3 business days.