Javascript for Beginners
Keywords used in JS:
In JavaScript, variables are declared using the keywords var, let, or const.
1. var Keyword
The var keyword is used to declare a variable. It has a function-scoped or globally-scoped behavior.
var n = 5; //declaring and initializing a variable
console.log(n);
var n = 20; // redeclaring and initializing is allowed
console.log(n);REMEMBER!!
It works both inside and outside functions, but the behavior changes.
var works:
- ✅ Inside functions → scoped to function
- ✅ Outside functions → global
- ✔
varinside function → accessible only within that function - ✔ but accessible everywhere inside that function (even across blocks)
- ❌ not accessible outside the function
When declared outside var is global / declared inside function then accessible inside only function, cannot access outside of it
function test() {
var x = 10;
console.log(x); // ✅ works
}
test();
console.log(x); // ❌ Error: x is not defined✅ Why?
Because var has function scope, meaning:
- It is available anywhere inside the same function
- It is not available outside that function
function test() {
if (true) {
var x = 10;
}
console.log(x); // ✅ 10 (still accessible)
}
``
Even though x is inside if, it’s still accessible in the function
beacuse var ignores block scope. Hence it is accessible2. let Keyword
The let keyword is introduced in ES6, has block scope and cannot be re-declared in the same scope/
let n= 10; //declarng variable
n = 20; // Value can be updated
// let n = 15; //can not redeclare SyntaxError: Identifier 'name' has already been declared
console.log(n)3. const Keyword
The const keyword declares variables that cannot be reassigned. It’s block-scoped as well.
You must assign a value when declaring a variable.
const n; // ❌ Error
const n = 100;
// n = 200; This will throw an error
console.log(n)var vs let vs const
JavaScript Data Type Categories
JavaScript data types are categorized into Primitive and Non-Primitive types
Primitive Data Type
Primitive data types in JavaScript represent simple, immutable values stored directly in memory, ensuring efficiency in both memory usage and performance.
- Primitive Data Types: Includes Number, String, Boolean, Null, Undefined, BigInt, and Symbol
Numbers:
Variables are containers that hold data values. They are used to store, manipulate, and display information within a program.
Get Raman M Bellary’s stories in your inbox
Join Medium for free to get updates from this writer.
In short, a variable is like a memory unit that we can access by typing the name of the variable.
Each variable has a unique name and a value that can be of different types. JavaScript is capable of automatically detecting the variable type, which makes coding more efficient.
To initialize a variable, we use the following format:
let variable_name = valueLet’s take a look at the different types of numbers:
JavaScript has a number type that can represent both integers and floating-point numbers, which means it treats all numbers types such as integers, float or real numbers as number
number - can be a whole number, such as 1 or -2, or a real number, such as 1.32 or 0.98.
For example:
To initialize a variable of type number with the name a and the value 3:
let a = 3To initialize a variable of type number with the name b and the value 13.2:
let b = 13.2Strings:
char is a single character (For example: 1, 6, %, b, p, ., T, etc.) — note that char is used here as a conceptual term; JavaScript does not have a dedicated char type.
The string type consists of one or more chars — a string can be a single character or many characters combined.
To initialize a string value in a variable, enclose it within single or double quotation marks:
let s1 = 'This is a string'
let s2 = "This is also a string"In the above example, two string variables are initialized, named s1 and s2.
Template Literals (Recommended): Use backticks (`) and ${} to embed variables directly into a string.
const name = "Raman";
const age = 25;
console.log(`Hello, ${name}`); // Output: Hello, Raman!
console.log(`Hello, "${name}"`);// Output: Hello, "Raman"
console.log(`User: ${name}, Age: ${age}`);
// Output: User: Alex, Age: 25
firstName = "John"
lastName = "Doe"
console.log(`firstName = ${firstName}, lastName = ${lastName}`)
// Output: firstName = John, lastName = Doe
Comma Separation: You can pass multiple arguments; JavaScript will automatically add a space between them.
let name = "Raman";
let age = 25;
let isProgrammer = true;
console.log("User:", name, "Age:", age, "Developer:", isProgrammer);
// Output: User: Raman Age: 25 Developer: trueBoolean:
A Boolean (or bool) is a data type that has only two possible values:
truefalse
Boolean keywords must be lowercase in JavaScript.
You can store a Boolean value in a variable like this:
let isRaining = true;
let hasAccess = false;JavaScript is case-sensitive. Only lowercase true/false is a valid boolean keyword; True/False would be treated as an undefined variable.





