INTRODUCTION TO JAVASCRIPT
JavaScript is a high-level, dynamic programming language commonly used to create interactive effects within web browsers.
Key Concepts:
- Variables and Data Types
- Operators
- Control Flow (Conditionals and Loops)
- Functions
- Objects and Arrays
- Event Handling
VARIABLES AND DATA TYPES
VARIABLES
Variables are containers for storing data values.
Syntax:
javascriptCopy codelet x = 5;
const y = "Hello";
var z = true;
Data Types:
- Number: Represents numeric values.javascriptCopy code
let age = 25;let price = 19.99;
- String: Represents text.javascriptCopy code
let name = "John";
- Boolean: Represents logical values, true or false.javascriptCopy code
let isStudent = true;
- Array: Represents a collection of items.javascriptCopy code
let colors = ["red", "green", "blue"];
- Object: Represents a collection of properties.javascriptCopy code
let person = { firstName: "Jane", lastName: "Doe", age: 30};
OPERATORS
Operators are used to perform operations on variables and values.
Examples:
- Arithmetic Operators:
+
,-
,*
,/
,%
- Assignment Operators:
=
,+=
,-=
,*=
,/=
- Comparison Operators:
==
,===
,!=
,!==
,<
,>
,<=
,>=
- Logical Operators:
&&
,||
,!
CONTROL FLOW
CONDITIONAL STATEMENTS
Used to perform different actions based on different conditions.
Examples:
javascriptCopy codeif (age > 18) {
console.log("Adult");
} else {
console.log("Minor");
}
LOOPS
Used to execute a block of code a number of times.
Examples:
- for loop:javascriptCopy code
for (let i = 0; i < 5; i++) { console.log(i);}
- while loop:javascriptCopy code
let i = 0;while (i < 5) { console.log(i); i++;}
FUNCTIONS
Functions are blocks of code designed to perform a particular task.
Syntax:
javascriptCopy codefunction greet(name) {
return "Hello " + name;
}
console.log(greet("Alice"));
OBJECTS AND ARRAYS
OBJECTS
Objects are collections of properties.
Example:
javascriptCopy codelet car = {
make: "Toyota",
model: "Corolla",
year: 2020
};
console.log(car.make); // Outputs: Toyota
ARRAYS
Arrays are used to store multiple values in a single variable.
Example:
javascriptCopy codelet fruits = ["apple", "banana", "cherry"];
console.log(fruits[0]); // Outputs: apple
EVENT HANDLING
JavaScript can respond to user actions such as clicks and key presses.
Example:
javascriptCopy codedocument.getElementById("myButton").onclick = function() {
alert("Button clicked!");
};
SAMPLE QUESTIONS AND EXPLANATIONS
- Question: What are the main data types in JavaScript and how would you declare a variable for each type?Answer:
- Number:
let age = 25;
- String:
let name = "John";
- Boolean:
let isStudent = true;
- Array:
let colors = ["red", "green", "blue"];
- Object:
let person = { firstName: "Jane", lastName: "Doe", age: 30 };
- Number:
- Question: What is the difference between
let
,const
, andvar
?Answer:let
allows you to declare variables that can be reassigned.const
is used to declare variables that cannot be reassigned.var
is the old way to declare variables and is function-scoped.
- Question: Write a function in JavaScript that takes two numbers and returns their sum.Answer:javascriptCopy code
function add(a, b) { return a + b;}
console.log(add(5, 3)); // Outputs: 8