JAVASCRIPT
In JavaScript, an arrow function is a concise way of writing a function expression. It was introduced in ES6 and provides a shorter syntax compared to traditional function expressions. Arrow functions are often used for creating anonymous functions or for defining functions with a shorter and more readable syntax. Here's an example of an arrow function and its equivalent traditional function expression:
Arrow Function:
// Arrow function syntax: (parameters) => { function body }
// Example 1: A simple arrow function that adds two numbers
const add = (a, b) => {
return a + b;
};
// Example 2: Arrow function with implicit return (for one-liner functions)
const square = (num) => num * num;
// Example 3: Arrow function with no parameters
const sayHello = () => {
console.log("Hello!");
};
// Example 4: Arrow function as a callback
const numbers = [1, 2, 3, 4];
const squaredNumbers = numbers.map((num) => num * num);
console.log(squaredNumbers); // Output: [1, 4, 9, 16]
Equivalent Traditional Function Expression:
// Traditional function expression syntax: function(parameters) { function body }
// Example 1: Equivalent traditional function for adding two numbers
const add = function(a, b) {
return a + b;
};
// Example 2: Equivalent traditional function with explicit return (for one-liner functions)
const square = function(num) {
return num * num;
};
// Example 3: Equivalent traditional function with no parameters
const sayHello = function() {
console.log("Hello!");
};
// Example 4: Equivalent traditional function as a callback
const numbers = [1, 2, 3, 4];
const squaredNumbers = numbers.map(function(num) {
return num * num;
});
console.log(squaredNumbers); // Output: [1, 4, 9, 16]
what is instructor in Js:
In simple terms, a constructor in JavaScript is like a blueprint for creating objects with certain properties and behaviors. It's like a recipe that you can use to make multiple objects that share the same characteristics.
Example:
Let's say we want to create multiple cars with different colors and models. We can use a constructor to define the blueprint for creating these cars:
// Constructor for creating Car objects
function Car(color, model) {
this.color = color;
this.model = model;
this.start = function() {
console.log(`The ${this.color} ${this.model} is starting.`);
};
}
// Creating instances of the Car class using the constructor
const car1 = new Car("blue", "SUV");
const car2 = new Car("red", "sedan");
// Using the start method of the car objects
car1.start(); // Output: The blue SUV is starting.
car2.start(); // Output: The red sedan is starting.
In this example, the Car constructor function takes two parameters (color and model) and assigns them as properties to the created objects using this. Each car object created with the Car constructor will have its own color, model, and the start method.
In JavaScript, data types are mutable values that are used in programming languages. JavaScript has many built-in data types, including:
Number: Used to represent a number such as 1, 3.14, or -10.
String: "Hello world!" This is used to represent text in a sentence or two, such as "JavaScript".
Boolean: Represents true or false. used in functions and conditions.
Null: Indicates that no value was created intentionally.
Undefined: Indicates a variable that has been declared but not yet assigned a value.
Object: A complex data type that can contain many values and objects. Objects can be created using curly braces {}.
Operators, on the other hand, are symbols or elements used to control values and variables. JavaScript provides many operators, including:
1.Arithmetic operators: addition (+), subtraction (-), equals (*), division (/), etc. It is used for basic mathematic calculations.
2.Assignment Operators: It is used to assign values to variables like the equal sign (=) or +=, -= , *= etc.
3.Comparison operators: equal (==), not equal (!=), greater than (>), less than (<), etc.
4.Logical operators: Used to perform logical operations on Boolean values such as AND (&&), OR () and NOT (!).
5.Unary operators: Used to perform single operations such as the increment (++) and decrement (--) operators.
Mastering JavaScript: Unleashing the Power of If-Else Conditions for Web Development:
In JavaScript, an if-else statement allows you to make decisions and execute different blocks of code based on a condition. It follows a simple structure:
if (condition) {
// Code to execute if the condition is true
} else {
// Code to execute if the condition is false
}
The condition is a logical expression that evaluates to either true or false. If the condition is true, the code within the first block will run. Otherwise, the code within the else block will execute.
If you have multiple conditions to check, you can use else-if statements:
if (condition1) {
// Code to execute if condition1 is true
} else if (condition2) {
// Code to execute if condition1 is false and condition2 is true
} else {
// Code to execute if both condition1 and condition2 are false
}
By utilizing if-else statements and conditions, you can create dynamic and responsive JavaScript code that performs different actions based on specific criteria.
file Name: TUT51IFELSE.HTML
<!DOCTYPE html>
<html lang="en">
<head>
//1. DIFF B/W 'let', 'var' & 'const'.
//2. IF ELSE CONDITION
//3. SWITCH CUPS CASES
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>scope, and conditions</title>
</head>
<body>
<!-- div>ul>li{item-$}*7 -->
<div>
<ul>
<li>item-1</li>
<li>item-2</li>
</ul>
</div>
<script>
//1. DIFF B/W 'let', 'var' & 'const'.
/*var string1= 'this is string1';
var string1= 'this is string2';
var string1='this is string3';
console.log(string1);*/
/* let a= "U";
//let a= "sk"; shows error - we can't take variable 'a' again if we use 'let'
console.log(a);
// for use variable 'a' again we must use under {} braces as below.
{ let a = "U8";
console.log(a);} */
/*const a ='This cannot be changed';
// a = "I want to change this . This cannot be changed.";
console.log(a); */
//2. IF ELSE CONDITION
let age = 5;
if (age >= 18) {
console.log("YOU'RE ELIGIBLE FOR VOTING");
}
else {
console.log("SORRY, YOU'RE NOT ELIGIBLE FOR VOTING");
}
/* let age= 5;
if(age>18){
console.log("you should drink water");
}
else if(age==5){
console.log("You're eligible so, you can play in water");
}
else{
console.log("you can drink cold water");
} */
//3. SWITCH CUPS CASES
// break nhi lagane par ye value ke bad bale sare cases ko run kr dega
/* const cups =42;
switch (cups) {
case 41:
console.log("The value of cups is 41");
break;
case 42:
console.log("The value of cups is 42");
break;
case 47:
console.log("The value of cups is 47");
break;
case 44:
console.log("The value of cups is 44");
break;
default:
console.log(" Sorry, The value of cups is none of 41,42,47,44.");
break;
} */
</script>
</body>
</html>
OUTPUT:
SORRY, YOU'RE NOT ELIGIBLE FOR VOTING
In JavaScript, an object is a fundamental data structure used to represent and organize data in a structured manner. It can be thought of as a container that holds related information, similar to real-life objects that have properties and behaviors.
An object in JavaScript consists of key-value pairs, where each key represents a property name, and each value represents the data associated with that property. The key-value pairs are enclosed in curly braces `{ }`.
For example, let's say we want to create an object to represent a person:
let person = {
name: "John",
age: 30,
occupation: "Developer"
};
In JavaScript, an array is a versatile data structure used to store multiple values in a single variable.
It allows you to group related data together and perform various operations on them.
To create an array, you can use square brackets and separate the values with commas:
let myArray = [value1, value2, value3];
Arrays can store any type of data, including numbers, strings, objects, and even other arrays. The elements in an array are accessed using their index, which starts from 0 for the first element:
let firstElement = myArray[0];
Arrays have several built-in methods that enable powerful data manipulation. Some commonly used methods include:
push: Adds elements to the end of an array.
pop: Removes the last element from an array.
length: Returns the number of elements in an array.
indexOf: Returns the index of a specified element in an array.
Here's an example showcasing these methods:
let fruits = ["apple", "banana", "orange"];
fruits.push("mango"); // Adds "mango" to the end of the array
console.log(fruits);
fruits.pop("orange"); // Removes the last element ("orange") from the array
console.log(fruits);
console.log(fruits.length); // Outputs 3
console.log(fruits.indexOf("banana")); // Outputs 1
Arrays are incredibly useful in JavaScript for tasks such as storing lists of data, iterating over elements, sorting, filtering, and much more. They provide a flexible and efficient way to work with collections of values in your JavaScript programs.
file Name: TUT52ARRAY.HTML
<!DOCTYPE html>
<html lang="en">
<head>
//1.OBJECT
//2.ARRAY
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ARRAY AND OBJECTS IN JS</title>
</head>
<body>
<div class="container">simple si html45</div>
<script>
let var1 = 45;
let var2 = "string";
let var3 = true;
let var4 = null;
let var5 = undefined;
console.log(var3)
//1.OBJECT
/* let employee = {
name: "Saurav",
salary: 10,
channel1: "CODE WITH SAURAV",
channel2: "Programmingwithsaurav1",
"channel 2": "Programmingwithsaurav",
"channel 1": "myelevencoder.blogspot "
}
console.log(employee) */
//2.ARRAY
let names = [1, 2, 3, "string", "array", undefined];
// let names= new Array(51, 2,3,"strings", undefined);
// let names= new Array(34,45);
// let names= new Array(34);
console.log(names[1]);
// console.log(names.length);
console.log(names[3].length);
names = names.sort();
names.push("This is pushed");
console.log(names);
</script>
</body>
</html>
OUTPUT:
Comments