JavaScript Fundamentals: Getting Started
Learn the fundamentals of JavaScript - variables, data types, operators, and basic syntax. Essential foundation for React development.
Topics Covered:
Prerequisites:
- Basic HTML & CSS knowledge
Video Tutorial
Overview
JavaScript is a versatile programming language that powers interactive web applications. Understanding JavaScript fundamentals is essential before learning React, as React is built on JavaScript. This tutorial covers the core concepts you need: variables, data types, operators, and basic syntax. You'll learn how to write JavaScript code, understand different data types, and use operators to manipulate values.
What is JavaScript?
JavaScript is a high-level, interpreted programming language that makes web pages interactive. It's one of the core technologies of the web, alongside HTML and CSS. Key Characteristics: • Runs in the browser (client-side) • Can also run on servers (Node.js) • Dynamic and loosely typed • Supports multiple programming paradigms • Essential for modern web development Why Learn JavaScript: • Required for React development • Powers interactive web applications • Most popular programming language • Runs everywhere (browser, server, mobile) • Large ecosystem and community
// JavaScript can be embedded in HTML
<script>
console.log("Hello, JavaScript!");
</script>
// Or in separate .js files
// script.js
console.log("Hello from external file!");JavaScript can be written inline in HTML using <script> tags or in separate .js files. The console.log() function outputs messages to the browser console.
Variables: let, const, and var
Variables store data that can be used and changed throughout your program. JavaScript has three ways to declare variables. Variable Declaration: • let - Block-scoped, can be reassigned • const - Block-scoped, cannot be reassigned (constant) • var - Function-scoped, older way (avoid in modern code) Best Practices: • Use const by default • Use let when you need to reassign • Avoid var (legacy) • Use descriptive names • Follow camelCase convention
// Using const (preferred for values that don't change)
const name = "Alice";
const age = 30;
// name = "Bob"; // ❌ Error: Cannot reassign const
// Using let (for values that change)
let count = 0;
count = 1; // ✅ OK
count = count + 1; // ✅ OK
// Using var (avoid in modern JavaScript)
var oldWay = "Don't use this";
// var is function-scoped, not block-scoped
// Variable naming
const firstName = "John"; // ✅ Good: camelCase, descriptive
const x = 5; // ❌ Bad: not descriptive
const user_name = "Jane"; // ❌ Bad: use camelCase, not snake_caseUse const for values that don't change, let for values that do change. Avoid var. Use descriptive, camelCase names for variables.
Data Types in JavaScript
JavaScript has several built-in data types. Understanding these is crucial for working with data. Primitive Types: • string - Text data • number - Numbers (integers and decimals) • boolean - true or false • undefined - Variable declared but not assigned • null - Explicitly empty value • symbol - Unique identifier (advanced) Special Values: • null - Intentional absence of value • undefined - Variable not initialized • NaN - Not a Number (result of invalid math) Type Checking: • Use typeof operator • JavaScript is dynamically typed • Types are determined at runtime
// String
const name = "Alice";
const greeting = 'Hello';
const template = `Hello, ${name}!`; // Template literal
// Number
const age = 30;
const price = 19.99;
const negative = -5;
// Boolean
const isActive = true;
const isComplete = false;
// Undefined
let value; // undefined
console.log(value); // undefined
// Null
const empty = null; // Explicitly empty
// Type checking
console.log(typeof name); // "string"
console.log(typeof age); // "number"
console.log(typeof isActive); // "boolean"
console.log(typeof value); // "undefined"
console.log(typeof empty); // "object" (quirk of JavaScript)
// Type conversion
const numString = "42";
const num = Number(numString); // Convert to number
const str = String(42); // Convert to string
const bool = Boolean(1); // Convert to boolean (true)JavaScript has several data types. Use typeof to check types. JavaScript automatically converts types in some situations (type coercion).
Operators
Operators perform operations on values. JavaScript has many operators for different purposes. Arithmetic Operators: • + Addition • - Subtraction • * Multiplication • / Division • % Modulo (remainder) • ** Exponentiation Comparison Operators: • == Equal to (loose) • === Equal to (strict) • != Not equal (loose) • !== Not equal (strict) • > Greater than • < Less than • >= Greater than or equal • <= Less than or equal Logical Operators: • && AND • || OR • ! NOT Assignment Operators: • = Assignment • += Add and assign • -= Subtract and assign
// Arithmetic operators
const sum = 10 + 5; // 15
const difference = 10 - 5; // 5
const product = 10 * 5; // 50
const quotient = 10 / 5; // 2
const remainder = 10 % 3; // 1
const power = 2 ** 3; // 8
// Comparison operators
console.log(5 == "5"); // true (loose equality)
console.log(5 === "5"); // false (strict equality - preferred)
console.log(5 !== "5"); // true
console.log(10 > 5); // true
console.log(10 < 5); // false
// Logical operators
const isAdult = true;
const hasLicense = false;
console.log(isAdult && hasLicense); // false (both must be true)
console.log(isAdult || hasLicense); // true (either can be true)
console.log(!isAdult); // false (negation)
// Assignment operators
let count = 5;
count += 3; // count = count + 3 (8)
count -= 2; // count = count - 2 (6)
count *= 2; // count = count * 2 (12)
count /= 3; // count = count / 3 (4)Operators perform operations on values. Use === for strict equality checks. Logical operators combine boolean values. Assignment operators modify variables.
Working with Strings
Strings are sequences of characters. JavaScript provides many ways to work with strings. String Creation: • Single quotes: 'text' • Double quotes: "text" • Template literals: `text` (ES6+) String Methods: • length - Get string length • toUpperCase() - Convert to uppercase • toLowerCase() - Convert to lowercase • includes() - Check if contains substring • indexOf() - Find position of substring • slice() - Extract portion of string • replace() - Replace substring Template Literals: • Use backticks (`) • Allow interpolation with ${} • Support multi-line strings
// String creation
const name1 = 'Alice';
const name2 = "Bob";
const name3 = `Charlie`; // Template literal
// String properties and methods
const text = "Hello World";
console.log(text.length); // 11
console.log(text.toUpperCase()); // "HELLO WORLD"
console.log(text.toLowerCase()); // "hello world"
console.log(text.includes("World")); // true
console.log(text.indexOf("World")); // 6
console.log(text.slice(0, 5)); // "Hello"
console.log(text.replace("World", "JavaScript")); // "Hello JavaScript"
// Template literals (ES6+)
const firstName = "Alice";
const age = 30;
const greeting = `Hello, my name is ${firstName} and I am ${age} years old.`;
// "Hello, my name is Alice and I am 30 years old."
// Multi-line strings with template literals
const message = `This is a
multi-line
string`;Strings can be created with quotes or template literals. Template literals allow interpolation and multi-line strings. Use string methods to manipulate text.
Control Flow: if/else and switch
Control flow allows your program to make decisions and execute different code based on conditions. if/else Statements: • Execute code conditionally • Can chain multiple conditions • Use logical operators for complex conditions switch Statements: • Alternative to multiple if/else • Compare value against multiple cases • Use break to prevent fall-through • Include default case Ternary Operator: • Shorthand for simple if/else • Syntax: condition ? valueIfTrue : valueIfFalse
// if/else statements
const age = 20;
if (age >= 18) {
console.log("You are an adult");
} else {
console.log("You are a minor");
}
// Multiple conditions
const score = 85;
if (score >= 90) {
console.log("Grade: A");
} else if (score >= 80) {
console.log("Grade: B");
} else if (score >= 70) {
console.log("Grade: C");
} else {
console.log("Grade: F");
}
// Logical operators in conditions
const isLoggedIn = true;
const hasPermission = false;
if (isLoggedIn && hasPermission) {
console.log("Access granted");
} else {
console.log("Access denied");
}
// switch statement
const day = "Monday";
switch (day) {
case "Monday":
console.log("Start of work week");
break;
case "Friday":
console.log("End of work week");
break;
default:
console.log("Mid week");
}
// Ternary operator
const age = 20;
const status = age >= 18 ? "adult" : "minor";
console.log(status); // "adult"Use if/else for conditional logic. Switch statements are useful for multiple value comparisons. Ternary operator provides concise conditional expressions.
Conclusion
JavaScript fundamentals are the foundation for React development. You've learned about variables (let, const), data types, operators, strings, and control flow. These concepts are essential for understanding React code. Practice writing JavaScript code and experiment with different operators and control structures. Next, you'll learn about functions, arrays, and objects - the building blocks of JavaScript applications.