Introduction to JavaScript: The Language of the Web

 

🟨 JavaScript Basics for Freshers



🧠 What is JavaScript?

JavaScript is a scripting language used to create interactive and dynamic content on websites. It runs directly in the web browser and can be used to:

  • ✅ Validate user input before sending data to the server
  • ✅ Add interactivity like form validation, image sliders, animations, etc.
  • ✅ Modify HTML & CSS while the page is running

⚡ JavaScript is client-side scripting, meaning the code runs in the user’s browser, not on the server.

🏢 Origin of JavaScript

  • Invented by Brendan Eich in 1995
  • Developed at Netscape Communications
  • First ran in the Netscape Navigator browser
  • Originally named Mocha, then LiveScript, and finally JavaScript

🖥️ What is Client-Side Scripting?

A client-side script runs on the user's device (browser) rather than on the server.

  • 🔸 It’s embedded in the HTML file
  • 🔸 No need for server-side interaction to execute
  • 🔸 Improves speed and responsiveness

💡 Example Tasks: Form validation, animations, dynamic page updates.

🏷️ How to Add JavaScript in HTML

<script type="text/javascript">
  // Your JavaScript code goes here
</script>
  

✅ Note: Modern browsers assume type="text/javascript" by default, so it’s optional.

🖨️ Displaying Output Using document.write()

document.write() is a built-in JavaScript method used to write directly to the web page.

🧪 Example:

<script>
  document.write("Hello, welcome to JavaScript!");
</script>
  

📝 Output:
Hello, welcome to JavaScript!

➕ Concatenation with + Operator

The + operator joins (concatenates) strings or text in JavaScript.

🔤 Example:

<script>
  document.write("Hello " + "World!");
</script>
  

📝 Output:
Hello World!

📌 Summary Table

Concept Description
JavaScript Scripting language for web interactivity
Runs On Browser (Client-side)
Invented By Brendan Eich at Netscape (1995)
Insert JS in HTML <script> tag
Output Function document.write()
String Concatenation + operator to join text
Usage Validation, animations, dynamic content updates

🟩 JavaScript Variables

In programming, a variable is a named container used to store data that can change (or vary) over time. Variables in JavaScript are declared before use and follow specific rules.

📌 Rules for Declaring Variables:

  • 1️⃣ Variable must be declared before use using the var keyword.
  • 2️⃣ Name can include letters (A-Z, a-z), digits (0-9), underscore (_), and dollar sign ($).
  • 3️⃣ Must begin with a letter, underscore, or dollar sign.
  • 4️⃣ Spaces are not allowed in variable names.
  • 5️⃣ JavaScript variables are case-sensitive. x and X are different.

🧪 Example:

var age = 28;
  
  • var – Declares a variable
  • age – Name of the variable
  • = – Assignment operator
  • 28 – Value assigned to variable

📍 After declaration, you can use the variable name throughout your code to reference its value.


🔷 Data Types in JavaScript

A data type defines the kind of data a variable can hold. JavaScript is a dynamically typed language, which means you don’t need to declare the data type when creating a variable — JavaScript figures it out automatically.

▶️ To check a variable's data type, use the typeof() function.

📦 Primitive Data Types:

  • 1. Number: Integers, floats, hexadecimal, etc.
    let num = 42;
  • 2. String: Sequence of characters enclosed in quotes.
    let name = "John";
  • 3. Boolean: Logical true or false.
    let isActive = true;
  • 4. Undefined: Variable declared but no value assigned.
    let x;
  • 5. null: Represents intentional absence of value.
    let value = null;

🧩 Non-Primitive Data Types:

  • 1. Object: Collection of key-value pairs.
    let person = { name: "Alice", age: 25 };
  • 2. Array: Ordered list of values.
    let colors = ["red", "green", "blue"];

🟪 let and const Keywords

Besides var, you can declare variables using let and const.

  • let: Declares block-scoped variables. You can reassign values.
    let score = 100;
  • const: Declares block-scoped constants. Value cannot be reassigned.
    const pi = 3.14;

➗ JavaScript Operators

Operators are symbols that tell JavaScript to perform specific operations such as addition, comparison, or logic-based decisions. JavaScript supports the following types of operators:

  • 1️⃣ Arithmetic Operators
  • 2️⃣ Relational (Comparison) Operators
  • 3️⃣ Logical Operators
  • 4️⃣ Assignment Operators
  • 5️⃣ String Operators
  • 6️⃣ Bitwise Operators
  • 7️⃣ Special Operators

➕ Arithmetic Operators

These perform basic mathematical operations using two operands.

OperatorDescriptionExampleResult
+Additiona = 10, b = 15
c = a + b
25
-Subtractionc = b - a5
*Multiplicationc = a * b150
/Divisionc = a / b0.666...
%Modulo (Remainder)c = 14 % 32
++Incrementa++11
--Decrementa--9

🔁 Relational Operators

Used for comparing two values. Result is either true or false.

OperatorDescriptionExampleResult
>Greater than15 > 10true
<Less than15 < 10false
==Equals to15 == 10false
>=Greater than or equal15 >= 10true
<=Less than or equal15 <= 10false
!=Not equal15 != 10true

🔀 Logical Operators

OperatorDescriptionExampleResult
&&ANDX && Ytrue if both are true
||ORX || Ytrue if one is true
!NOT!XInverts truth

📝 Assignment Operators

OperatorDescriptionExampleEquivalent
=Assignx = yx = y
+=Add & assignx += yx = x + y
-=Subtract & assignx -= yx = x - y
*=Multiply & assignx *= yx = x * y
/=Divide & assignx /= yx = x / y
%=Modulo & assignx %= yx = x % y

🔤 String Operators

OperatorDescriptionExampleResult
+Concatenation"Hello" + " World""Hello World"
+=Concatenation assignmentstr1 += str2Join str2 to str1

🧮 Bitwise Operators

SignOperatorDescription
&ANDReturns 1 if both bits are 1
|ORReturns 1 if either bit is 1
^XORReturns 1 if bits are different
~NOTInverts bits
<<Left ShiftMultiplies number by 2^shift
>>Right ShiftDivides number by 2^shift

✨ Special Operators

OperatorDescription
typeofCheck the type of a variable
instanceofCheck instance of object type
newCreate an object instance
deleteDelete object property
, (comma)Separates expressions
?:Conditional (ternary) operator

🔁 LOOP STATEMENT

Loop is a statement that repeats a block of statement until some condition is true. JavaScript supports the following loops:

  • For
  • While
  • Do..while
  • For...in

📌 For Loop

for(initialization; condition; update) {
  // code block
}

📌 While Loop

while(condition) {
  // code block
  update;
}

📌 Do...While Loop

do {
  // code block
} while(condition);

🧮 Function in JavaScript

A function is a reusable block of code defined using the function keyword:

function myFunction(param) {
  // statements
}

📲 HTML Events using JavaScript

HTML events provide dynamic interactions on webpages.

  • onclick: When element is clicked
  • onchange: When value of input changes
  • onmouseover: When mouse hovers over element
  • onmouseout: When mouse leaves element
  • onkeydown: When a key is pressed
  • onkeyup: When a key is released
  • onload: When page or element is loaded

🖱️ Onclick Example:

<button onclick="myFunction()">Click Me</button>

📝 Form Events

  • onfocus: When element receives focus
  • onblur: When element loses focus
  • onchange: When input value changes
  • onsubmit: When form is submitted

🌐 Open New Page

Use window.open() to open a new tab or window:

window.open("https://example.com", "_blank");

✅ Form Validation

Form validation ensures that users fill all required fields correctly before submitting.

  • Required field validation
  • Data format validation (e.g., email, number)

Post a Comment

0 Comments