The Language of the Web Keywords: JavaScript tutorial, what is JS, learn JavaScript for beginners, JavaScript basics What is JS? JavaScript (JS) is a high-level, interpreted programming language that brings interactivity and dynamic features to websitesHow Does a Computer Work? Understanding the Brain of Your Machine – 804. While HTML handles structure and CSS controls design, JS is responsible for behavior. Example: javascriptCopyEditalert(“Welcome to my website!”); When you run this code in a browser, youβll see a popup message.Learning Breeze β Learn Science with Ease A Brief History of JS Where is JS Used? How JS Works in the Browser Advantages of JS Limitations: Quick Start: Adding JS to HTML htmlCopyEdit<!DOCTYPE html> <html> <head> <title>JS Example</title> </head> <body> <script> document.write(“Hello, JavaScript!”); </script> </body> </html> In conclusion, JavaScript is a powerful and versatile programming language that plays a crucial role in modern web development. From creating dynamic user interfaces to enabling complex server-side applications through platforms like Node.js, JavaScript continues to evolve and expand its capabilities. Its widespread support, active developer community, and integration with HTML and CSS make it an essential tool for building interactive and responsive websites. Whether you’re a beginner or an experienced developer, learning JavaScript opens the door to endless possibilities in the digital world.
Variables in JavaScript: var, let, and const
Variables in JavaScript Keywords: JavaScript variables, var let const, JavaScript basics What are Variables? Variables are containers that store data for later use. Example: javascriptCopyEditlet name = “Ali”; const age = 25; var country = “Pakistan”; Three Ways to Declare Variables Keyword Scope Reassignment Hoisting Use Case var Function Yes Yes Legacy code let Block Yes No Changing values const Block No No Fixed values Variable Scope Example: javascriptCopyEdit{ let blockVar = “Inside Block”; console.log(blockVar); } // console.log(blockVar); // Error Hoisting JavaScript moves var declarations to the top before execution, but let and const are not accessible before YouTube declaration. Best Practices
Data Types in JavaScript
JavaScript data types, JavaScript string number boolean object Primitive Data Types Non-Primitive Data Types javascriptCopyEditlet person = { name: “Ali”, age: 25 }; javascriptCopyEditlet fruits = [“apple”, “banana”]; javascriptCopyEditfunction greet() { console.log(“Hi!”); } Type Conversion Implicit: javascriptCopyEditconsole.log(“5” + 2); // “52” Explicit: javascriptCopyEditNumber(“5”); // 5 String(123); // “123” typeof Operator javascriptCopyEdittypeof “Hello”; // string typeof 42; // number π JavaScript Data Types: Full Guide JavaScript is a dynamically typed language, meaning you donβt need to specify variable types. The type is determined automatically at runtime. πΉ 1. Primitive Data Types Primitive types are the most basic data types in JavaScript. They are immutable and represent a single value. π‘ a. Number π‘ b. String π‘ c. Boolean π‘ d. Undefined π‘ e. Null Note: typeof null returns “object” β this is a well-known quirk in JavaScript. π‘ f. Symbol (ES6) π‘ g. BigInt (ES11) πΈ 2. Non-Primitive (Reference) Data Types These types can store collections of values or more complex entities. They are mutable and stored by reference. π· a. Object π· b. Array π· c. Function Or as an expression: Or with arrow syntax: π· d. Date, RegExp, Map, Set, WeakMap, WeakSet, etc. These are built-in object types in JavaScript. π§ Dynamic Typing Example JavaScript allows you to change the type of a variable during runtime: π typeof Operator Use typeof to check the type of a variable: π Summary Table Type Category Example typeof result Number Primitive 42, 3.14, NaN “number” String Primitive “hello”, ‘world’ “string” Boolean Primitive true, false “boolean” Undefined Primitive let x; “undefined” Null Primitive null “object” Symbol Primitive Symbol(“id”) “symbol” BigInt Primitive 12345678901234567890n “bigint” Object Reference { name: “Alice” } “object” Array Reference [1, 2, 3] “object” Function Reference function() {} “function” β Best Practices
Functions in JavaScript
Keywords: JavaScript functions, JavaScript basics, create function JavaScript What is a Function? A reusable block of code for performing a task. Example: javascriptCopyEditfunction greet(name) { return `Hello, ${name}`; } console.log(greet(“Sara”)); Types of Functions javascriptCopyEditfunction add(a, b) { return a + b; } javascriptCopyEditconst multiply = function(a, b) { return a * b; }; javascriptCopyEditconst divide = (a, b) => a / b; Default & Rest Parameters javascriptCopyEditfunction sum(a = 1, b = 1) { return a + b; } function total(…nums) { return nums.reduce((a, b) => a + b); } Best Practices