Docsity

Master JavaScript:Complete Notes on Variables, Operators, Loops & Function |Any University, Summaries of Javascript programming

Toggle sidebar
bg1
Unit I - Basics of JavaScript Programming
TEACHING HOURS – 10 TOTAL MARKS – 12
Course Outcome (CO-1) – Create interactive web pages using program flow control
Structure.
1. What is JavaScript? State the need of JavaScript.
Explanation -
JavaScript is a programming language for webpage which is used both on the client-
side and server-side that allows you to make web pages interactive.
Need of JavaScript -
• JavaScript can calculate, manipulate and validate data.
• Where HTML and Cascading Style Sheet (CSS) are languages that give structure and
style to web pages, JavaScript gives web pages interactive elements that engage a
user.
2. Write Any Four Applications of JavaScript –
Explanation –
• Adding interactive behavior to web pages –
o Show or hide more information with the click of a button
o Change the color of a button when the mouse hovers over it
o Slide through a stack of images on the homepage
o Zooming in or zooming out on an image
o Displaying a timer or count-down on a website
o Playing audio and video in a web page
o Displaying animations
• Creating web and mobile apps –
o Developers can use various JavaScript frameworks for developing and building
web and mobile apps.
o Popular JavaScript front-end frameworks include React, React Native, Angular,
and Vue.
o A few famous examples include Paypal, LinkedIn, Netflix, and Uber.
• Building web servers and developing server applications –
o Beyond websites and apps, developers can also use JavaScript to build simple
web servers and develop the back-end infrastructure using Node.js.
• Game development –
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Partial preview of the text

Download Master JavaScript:Complete Notes on Variables, Operators, Loops & Function |Any University and more Summaries Javascript programming in PDF only on Docsity!

Unit I - Basics of JavaScript Programming TEACHING HOURS – 10 TOTAL MARKS – 12 Course Outcome (CO-1) – Create interactive web pages using program flow control Structure.

1. What is JavaScript? State the need of JavaScript. Explanation - JavaScript is a programming language for webpage which is used both on the client- side and server-side that allows you to make web pages interactive. Need of JavaScript - - JavaScript can calculate , manipulate and validate data. - Where HTML and Cascading Style Sheet (CSS) are languages that give structure and style to web pages, JavaScript gives web pages interactive elements that engage a user. 2. Write Any Four Applications of JavaScript – Explanation – - Adding interactive behavior to web pages – o Show or hide more information with the click of a button o Change the color of a button when the mouse hovers over it o Slide through a stack of images on the homepage o Zooming in or zooming out on an image o Displaying a timer or count-down on a website o Playing audio and video in a web page o Displaying animations - Creating web and mobile apps – o Developers can use various JavaScript frameworks for developing and building web and mobile apps. o Popular JavaScript front-end frameworks include React, React Native, Angular, and Vue. o A few famous examples include Paypal, LinkedIn, Netflix, and Uber. - Building web servers and developing server applications – o Beyond websites and apps, developers can also use JavaScript to build simple web servers and develop the back-end infrastructure using Node.js. - Game development –

o We can use JavaScript to create browser games. Some of the famous games are Pack-Man and Angry Birds.

3. What is JavaScript Interpreter – Explanation – - It is the part of the web browser that understands JavaScript and can execute the steps of a JavaScript program. - Web browsers are built to understand HTML and CSS and convert those languages into a visual display on the screen. - The web browser is usually expecting HTML, so you must specifically tell the browser when JavaScript is coming by using the tag as shown in the following example –

My First JavaScript Program - We can write JavaScript Program inside tag or tag or inside external file having .js extension.

5. State different features of JavaScript– Explanation –

Unit I - Basics of JavaScript Programming TEACHING HOURS – 10 TOTAL MARKS – 12 Course Outcome (CO-1) – Create interactive web pages using program flow control Structure.

7. State different types of values in JavaScript– Explanation – Any information present on the webpage between different html tags like , ,

etc., are called values. For example -

welcome to web 2020 Here, the text “ **welcome to web** ” and a number **2020** is a value Types of values in JavaScript are - - Number – any numeric value like 15, 16.35, - 745, 3.14159 etc - String – any text enclosed in double or single quote like “hello everyone” - Boolean – any non-zero value is true and a zero is false - Null – the absence of value is called as Null. - Object – Any object like document, window, form, is also a value. **8. How to declare variable in JavaScript? Explanation –** Variable can be declared in JavaScript using **var** , **let** or **const** keyword. - Variable declared with **var** keyword o Variable declared with var keyword can be re-declared. o It is does not have a block scope. o For example

  • Variable declared with let keyword o Variable declared with let keyword cannot be re-declared. o It has a block scope. o For example
  • Variable declared with const keyword o Variable declared with const keyword cannot be re-declared. o It has a block scope. o Value of const variable cannot be changed. o const variables must be assigned value at declaration. 9. Enlist different operators in JavaScript. Explanation –

10. Explain different decision making statements in JavaScript - Explanation – In decision making, we test a specific condition and execute one block of statement, if condition is true. If condition is false, we execute another block of statements. Different decision making statements in JavaScript are – o if statement o if … else statement o Nested if…else o else if statement o switch statement o Conditional statement (? : ) - If statement – We use “if statement” to specify a block of code to be executed if condition is true - If - else statement –

We use “if…else statement” to specify a block of code to be executed if condition is true. If condition is false then another block of statements is executed.

  • Nested If – else statement – We use “Nested if…else statement” to test another condition if first condition is true.
  • If – else if statement – We use “if…else if statement” to test another condition if first condition is false.

Unit I - Basics of JavaScript Programming TEACHING HOURS – 10 TOTAL MARKS – 12 Course Outcome (CO-1) – Create interactive web pages using program flow control Structure.

11. Explain different loop statements in JavaScript - Explanation – In JavaScript, loops are used to execute a block of code repeatedly until a certain condition is met. Following loops are available in JavaScript. While loop: It is used to repeat a given block of statements till a condition is being satisfied. Do - While loop: It is used to repeat a given block of statements till a condition is being satisfied.

For loop: It is used to repeat a given block of statements till a condition is being satisfied. For…in loop: For…in loop is used loop through the properties of an object.

Continue statement

  • The continue statement is used to skip the rest of the current iteration and move on to the next iteration of the loop.
  • It doesn't terminate the loop; instead, it skips the remaining code within the current iteration and proceeds to the next iteration. 13. What is Object? Explain Object initialization and manipulation in JavaScript – Explanation – Object
  • In JavaScript, an object is a fundamental data structure that allows you to store and organize related data and functionality.
  • Objects are used to represent real-world entities, concepts, or structures in your code.
  • They are made up of key-value pairs, where each key is a string (also called a property name), and each value can be of any data type, including other objects or functions. Creating Object
  • Object can be created using the object literal notation, which involves defining properties and methods directly within curly braces {}.

Modifying Object

  • Objects can be modified by adding, updating, or deleting properties and methods dynamically. You can do this using dot notation or square bracket notation:
  • pop() : Removes the last element from the array and returns it. o cars.pop();
  • unshift(element1, element2, ...) : Adds one or more elements to the beginning of the array. o cars.unshift(“Maruti”); // add an item to the front of an array o cars.unshift(“Tata”, “Mahindra”, “Jeep”) ;
  • shift() : Removes the first element from the array and returns it. o cars.shift(); Combining and Splitting Arrays:
  • concat(array1, array2, ...) : Returns a new array that's a concatenation of one or more arrays. o a1=[10,20,30,40]; o a2=[15,25,35,45]; o document.write(a1.concat(a2));
  • join(separator) : Joins all elements of an array into a string, using the specified separator. o a1=[10,20,30,40]; o document.write(a1.join("#")); Slicing and Splicing Arrays:
  • slice(startIndex, endIndex) : Returns a new array containing elements from the specified start index up to, but not including, the end index. o a1=[10,20,30,40,50,60,70,80]; o document.write(a1.slice(3,7));
  • splice(startIndex, deleteCount, element1, element2, ...) : Removes or replaces elements from the array starting at the specified index. o a1=[10,20,30,40,50,60,70,80]; o document.write(a1.splice(3,2,5,6,7,8)); o document.write(""+a1); o output - 10,20,30,5,6,7,8,60,70, Searching and Sorting:
  • indexOf(element, startIndex) : Returns the index of the first occurrence of the specified element, starting from the given index. o a1=[10,20,30,40,50,40,50,80]; o document.write(" 40 found at " + a1.indexOf(40)); o document.write(" 40 found at " + a1.indexOf(40,4)); o Output -

40 found at 3 40 found at 5

  • lastIndexOf(element, startIndex) : Returns the index of the last occurrence of the specified element, starting from the given index. o a1=[10,20,30,40,50,40,50,80]; o document.write(" 50 found at " + a1.lastIndexOf(50)); o document.write(" 50 found at " + a1.lastIndexOf(50,4)); o Output - 50 found at 6 50 found at 4
  • includes(element) : Determines whether the array contains a certain element. o a1=[10,20,30,40,50,40,50,80]; o document.write("50 found = "+a1.includes(50)); o document.write("55 found = "+a1.includes(55)); o Output - 50 found = true 55 found = false
  • sort(compareFunction) : Sorts the elements of an array in place and returns the sorted array. Compare function decides whether to sort in ascending or descending order. o sort((a,b) => a-b) – sort the array elements in ascending order o sort((a,b) => b-a) – sort the array elements in descending order o For example – ▪ var numbers = [50, 20, 80, 10, 30, 80]; ▪ numbers.sort((a, b) => a - b); ▪ document.write("Ascending order="+ numbers); ▪ numbers.sort((a, b) => b - a); ▪ document.write("Descending order="+ numbers); o Output ▪ Ascending order=10,20,30,50,80, Descending order=80,80,50,30,20,
  • reverse() : Reverses the order of the elements in the array. o For example - ▪ var numbers = [50, 20, 80, 10, 30, 80]; ▪ document.write("Before Reversal="+numbers); ▪ numbers.reverse(); ▪ document.write("After Reversal="+numbers); o Output –

Lecture Notes On Client Side Scripting (CSS-22519) Under MSBTE I-SCHEME Prepared By Prof. S.M. Inwate Unit 2 – Array, Function and String TEACHING HOURS – 10 TOTAL MARKS – 14 Course Outcome (CO-2) – Implement Arrays and Functions in JavaScript.

3. Explain Function with its declaration type. Explanation - In JavaScript, a function is a block of reusable code that performs a specific task or a set of tasks. Functions allow us to encapsulate a piece of code, give it a name, and then invoke (call) that code by referring to its name. This promotes code reusability, modularity, and maintainability in our programs. Function declaration syntax- function function_name(parameters) { Statements; return(value); } For example - function cube(number) { return(numbernumbernumber); } We can call a function by passing arguments to it as shown below- document.write(“Cube of 5 is “ + cube(5)); 4. Explain local variables and default parameters in function. Explanation - Local variables: - Variables declared inside a function are local variables. - They only exist within the scope of that function.

Lecture Notes On Client Side Scripting (CSS-22519) Under MSBTE I-SCHEME Prepared By Prof. S.M. Inwate For example – function add(a,b,c=0) { result=a+b+c; document.write(“Addition=”+result); } In this example, variables a, b, c and result are local variables. Default parameters:

  • We can assign default values to parameters.
  • If a value is not provided when calling the function, the default value is used. In the above example, parameter c is default parameter. 4. Explain different built-in functions in JavaScript. Explanation -
  1. eval(): The eval() function is used to evaluate JavaScript code in a string format. Syntax: eval(string) Example: console.log(eval('10 + 10'));
  2. isFinite(): The isFinite() global function checks whether the provided value is a finite number or not. This function returns false if the argument is NaN, positive infinity, or negative infinity; otherwise, it returns true. Syntax: isFinite(testValue) Example: document.write(isFinite(-100));
  3. isNaN(): The isNaN() method checks whether or not a value is NaN. If the specified value is NaN, return true; otherwise, return false. Syntax: isNaN(value) Example: document.write(isNaN('A'))