JavaScript Notes PDF
JavaScript Notes PDF
▪ The value of type attribute is text/javascript is the content type that provides
information to the browser about the data. It is the default type value.
Example-
<script type="text/javascript">
document.write("Welcome to the world of JavaScript");
</script> Prepared by: Amitabh Srivastava
document.write()
▪ document.write() in JavaScript is a function that is used to display some text in the browser
window.
▪ When document.write() function is used after the HTML is fully loaded, it will delete all the
contents of an existing HTML document and will show only contents that are inside
the document.write() function.
Comments in JavaScript-
To make any line of JavaScript as a comment, use double slash ( // ). If you want to
make multiple lines as a comment,Prepared
use by: /*Amitabh
andSrivastava
*/.
External JavaScript
We can create external JavaScript file and embed it in many html pages.
It provides code re-usability because single JavaScript file can be used in several html pages.
An external JavaScript file must be saved by .js extension.
Example-
sample.html: sample.js:
Solution-
Example-
const x = 10
x = 12 // will produce error ‘Assignment to constant variable’
We need to parse/type-cast the values entered by the user from string type to an integer type.
Example-
Example-
Example-1: Example-2:
Solution-
Solution-
Logic-
Solution-
Entry-Controlled loops: In this type of loops the test condition is tested before
entering the loop body. For Loop and While Loop are entry-controlled loops.
Exit-Controlled Loops: In this type of loops the test condition is tested or evaluated
at the end of loop body. Therefore, the loop body will execute at least once,
irrespective of whether the test condition is true or false. do – while Loop is an exit-
controlled loop. Let’s explore each of these loops one by one with examples.
Prepared by: Amitabh Srivastava
While Loop
▪ While loop allows code to be executed repeatedly based on a given Boolean
condition.
Syntax-
for (initialization condition; testing condition; increment/decrement)
{ Example-
Statements to be executed...
}
Example-
Example-
Example-
Example-
▪ indexOf()- returns the index of given character. If not found, returns -1.
Example-
Ascending order-
newarr.sort(function(a, b){return a-b})
Descending order-
newarr.sort(function(a, b){return b-a})
▪ The reverse() method reverses the elements' positions in the array so that the last
element goes into the first position and the first one to the last place.
Example-
▪ Spread operators are written using three consecutive dots (...), and they provide
us an easy way to break up an array or an object into its individual elements.
▪ Example-
Solution-
Solution-
Example-
Example-
▪ When JavaScript code is included in HTML, it reacts over these events and allow
us to execute required code or set of statements.
▪ Events are part of the Document Object Model (DOM) and every HTML
element contains a set of events which can trigger JavaScript Code.
▪ For example, when user clicks on an object in a browser, the “OnClick” Event
Handler is called. So you can easily add some code or set of codes to this
“OnClick” event which you want toby:execute
Prepared when this event occurs.
Amitabh Srivastava
JavaScript Event Types
➢ Mouse events-
▪ onClick, onMouseOver, onMouseOut, etc.
➢ Keyboard events-
▪ onKeyUp, onKeyDown.
➢ Form events-
▪ onFocus, onBlur, onSubmit, etc.
➢ Document evets-
▪ onLoad, onResize, etc. Prepared by: Amitabh Srivastava
JavaScript Event: Example-1
Example-
onClick='x=2+5;alert("Sum of 2 and 5 is: "+x)'
▪ The simple examples of an event is a user clicking the mouse or pressing a key on the
keyboard.
▪
So, there is an in-built function in JavaScript called addEventListener() which takes two
parameters, first is the event to listen for, and a second argument to be called whenever
the described event occurs.
Syntax-
element.addEventListener(event, listener / handler function);
Example-
▪ endsWith() method is used to check if a string ends with the characters of another string. It
returns true if a string ends with the characters of a specified string, otherwise returns false. It is
also a case-sensitive method.
Syntax- String.endsWith(searchString [,length])
• searchString is the characters to be searched for at the end of the string.
• length is an optional parameter that determines the length of the string to search. It defaults
to the length of the string.
Prepared by: Amitabh Srivastava
Example: StartsWith() / endsWith()
Syntax- string.repeat(count);
▪ count is an integer value which represents the number of times to repeat the
given string. The range of the integer count is from zero (0) to infinite times.
Example-
Example-
Example-
▪ These functions are often defined inline and can be assigned to a variable or
passed as an argument to another function.
▪ Example-
An arrow function-
Syntax- function_variable = (arguments) => {
statements;
return value; }
Solution-
▪ It will also install ‘npm’ and ‘npx’ utility commands. To check whether ‘npm’ (Node Package
Manager) is installed correctly, just give-
▪ REPL stands for Read-Eval-Print-Loop. It provides a quick and easy way to test
simple Node.js/JavaScript code.
▪ You can also define variables and perform some operation on them.
▪ The REPL terminal will display three dots (...), it means you can continue
on next line. Write .break to get out of continuity mode.