Posts
Showing posts from October, 2023
CHAPTER - 18
- Get link
- X
- Other Apps
String in Javascript The String object is used for storing and manipulating text. A string simply stores a series of characters like "John Doe". A string can be any text inside quotes. You can use single or double quotes: Example: var carname="My Name is Khan"; You can access each character in a string with its position Example: var character=carname[7]; String Length <!DOCTYPE html> <html> <body> <script> var txt = "Hello World!"; document.write("<p>" + txt.length + "</p>"); var txt="ABCDEFGHIJKLMNOPQRSTUVWXYZ"; document.write("<p>" + txt.length + "</p>"); </script> </body> </html> Finding a String in a String The indexOf() method returns the position (as a number) of the first found occurrence of a specified text inside a string: Example var str="Hello world, welcome to the universe welcome."; var n=str.indexOf("welcome...
CHAPTER - 17
- Get link
- X
- Other Apps
Date and Time The Date object has been created, and now we have a variable that holds the current date! To get the information we need to print out, we have to utilize some or all of the following functions: getTime() - Number of milliseconds since 1/1/1970 @ 12:00 AM getSeconds() - Number of seconds (0-59) getMinutes() - Number of minutes (0-59) getHours() - Number of hours (0-23) getDay() - Day of the week(0-6). 0 = Sunday, ... , 6 = Saturday getDate() - Day of the month (0-31) getMonth() - Number of month (0-11) getFullYear() - The four digit year (1970-9999) <h4>It is now <script type="text/javascript"> <!-- var currentTime = new Date() var hours = currentTime.getHours() var minutes = currentTime.getMinutes() if (minutes < 10){ minutes = "0" + minutes } document.write(hours + ":" + minutes + " ") if(hours > 11){ document.write("PM") }else{ document.write("AM") } //--> </script> </h4>
CHAPTER - 16
- Get link
- X
- Other Apps
Javascript Redirect <html> <head> <script type="text/javascript"> function delayer(){ window.location = "http://www.google.com" } </script> </head> <body onLoad="setTimeout('delayer()', 5000)"> <h2>Prepare to be redirected!</h2> <p>This page is a time delay redirect, please update your bookmarks to our new location!</p> </body> </html>
CHAPTER - 14
- Get link
- X
- Other Apps
Confirm in javascript <html> <head> <script type="text/javascript"> function confirmation() { var answer = confirm("Do you want to leave this page?") if (answer==true){ window.location = "http://www.google.com/"; } else{ alert("Thanks for sticking around!") } } </script> </head> <body> <form> <input type="button" onclick="confirmation()" value="Leave This Page"> </form> </body> </html>
CHAPTER - 13
- Get link
- X
- Other Apps
Object In Javascript Objects are also a kind of variable but the difference between a normal variable and an object is that an object can hold many values while a variable can hold only one value. The object value is always in the key-value pair. Example: var languages = { ‘Graphic’:’Photoshop’, ‘Coding’:’javascript’, ‘desktopApplication’:’Visual Basic’ }; Now if you want to display the value of any object element, you can do this. console.log(languages.Graphic); Result: Photoshop
CHAPTER - 12
- Get link
- X
- Other Apps
ARRAY IN JAVASCRIPT An array is a variable that can store many variables within it. Many programmers have seen arrays in other languages, and they aren't that different in JavaScript. The following points should always be remembered when using arrays in JavaScript: 1. The array is a special type of variable. 2. Values are stored in an array by using the array name and by stating the location in the array you wish to store the value in brackets. Example: myArray[2] = "Hello World"; 3. Values in an array are accessed by the array name and location of the value. Example: myArray[2]; 4. JavaScript has built-in functions for arrays, so check out these built-in array functions before writing the code yourself! Example 1 : <script type="text/javascript"> var arr = new Array(); // constructor Array arr[0] = "Football"; arr[1] = "Baseball"; arr[2] = "Cricket"; document.write(arr[0] + arr[1] + arr[2]); //We can access the value using ...
CHAPTER - 11
- Get link
- X
- Other Apps
A function is a piece of code that can be called anywhere in the program. The function lies idle until it is referenced or called upon to do its work. Functions are time savers because the same piece of code you do not have to write again and again. There are three types of functions 1. Function without parameters / Argument 2. Functions with parameters 3. Function with return value 1. Example : <script> function callme(){ alert("I am being called from function"); } </script> <input type="button" name="Submit" value="Submit" onclick="callme();"> 2. Example : <script> function add(a,b){ //alert("I am being called from function"); document.write(a+b); } </script> <input type="button" name="Submit" value="Submit" onclick="add(30,20);"> 3. Example : <!DOCTYPE html> <html> <body> <p id="demo"></p> <script> fu...
CHAPTER - 10
- Get link
- X
- Other Apps
JAVASCRIPT SWITCH CASE If there are two or three statements then it's easy to manage through if and else. But if the condition is large then it's better to use a switch case. <!DOCTYPE html> <html> <body> <h2>JavaScript switch</h2> <p id="demo"></p> <script> let day; switch (new Date().getDay()) { case 0: day = "Sunday"; break; case 1: day = "Monday"; break; case 2: day = "Tuesday"; break; case 3: day = "Wednesday"; break; case 4: day = "Thursday"; break; case 5: day = "Friday"; break; case 6: day = "Saturday"; } document.getElementById("demo").innerHTML = "Today is " + day; </script> </body> </html>
CHAPTER - 9
- Get link
- X
- Other Apps
JavaScript Loops Loop mainly contains three parts 1. Initialization 2. Condition 3.Increment or decrement WHILE LOOP <script> var i=0; // initialization while(i<10){ // condition document.write(i’<br>’); i++; //increment } </script> DO WHILE LOOP What is the difference between While Loop and Do While Loop? Everything is the same but the only difference is that the while loop condition is checked before while the Do While Loop condition is checked later. So, even the condition is false in the case of Do While Loop at least once the program executes. <script> var i=11; do{ document.write(i+’<br>’); i++; }while(i<11); //The condition is false because its starting from 10 and it is increasing by one then it cannot be less than 11 </script> FOR LOOP in the for loop, all the statements are nested at one place <script> for(i=0;i<=10;i++){ document.write(i+’<br>’); } </script>
CHAPTER - 8
- Get link
- X
- Other Apps
JAVASCRIPT CONDITIONAL STATEMENTS IF, ELSE, ELSE IF <!DOCTYPE HTML> <html> <body> <script> var a=20; var b=30; if(a>b){ document.write(‘A is greater than B’); } else{ document.write(‘B is greater than A’); } </script> </body> </html> ELSE IF <!DOCTYPE html> <html> <body> <script> var a=20; var b=30; var c=40; if(a>b && a>c){ document.write(‘A is greater than B and C’); } else if(b>a && b>c){ document.write(‘B is greater than A and B is greater than C’); } else{ document.write(‘C is greater than A and B’); } </script> </body> </html>
CHAPTER - 7
- Get link
- X
- Other Apps
JAVASCRIPT OPERATORS Operators in JavaScript are very similar to operators that appear in other programming languages. The definition of an operator is a symbol that is used to perform an operation. Most often these operations are arithmetic (addition, subtraction, etc.), but not always. Javascript arithmetic operator chart Operator English Example + Addition 2+2=4 - Subtraction 4-2=2 * Multiplication 2*2=4 / Division 2/2=0 % Modulo 3%2=1 COMPARISON OPERATORS Operator English Example Result == Equal x==y true != Not Equal x!=y false < Less than x<y true > Greater than x>y false <= Less than equals to x<=y true >= Greater than equal to x>=y false SIMPLE PROGRAM <!DOCTYPE html> <html> <body> <h1>FIRST JAVASCRIPT PAGE</h1> <script> document.write("<p>My First JavaScript</p>"); </script> </body> </html> LOGICAL OPERATORS Operator English Example && AND (x < 10 && y > 1)...
CHAPTER - 6
- Get link
- X
- Other Apps
All programming languages in the world have data types. So, JavaScript is not an exception. Javascript also has pre-defined or built-in data types as well as user-defined data types. But JavaScript is a loosely typed language. That means one does not need to mention data types specifically. In the case of Java, C++ and C, one has to define data type specifically and only that kind of data can be stored in that variable. Statically Typed language Must define the data type of the variable before using it. C, C++ Java, Typescript Int a=10; a=’Rajesh’ // Error String name = 10; Dynamic Typed Language In Javascript var a =10 // fine a =”Rajesh” // fine There are the following types of data types in Javascript Boolean: true/false Null: null Undefined: undefined Number: 10,20 etc String: “Hello” Object: obj={}Symbol: Symbol
CHAPTER - 5
- Get link
- X
- Other Apps
WHAT IS VARIABLE? A variable's purpose is to store information so that it can be used later. A variable is a symbolic name that represents some data that you set JavaScript variables can be used to hold values. (x=5) or expressions (z=x+y) var a=30; a=50; Variable names must begin with a letter Variable names can also begin with $ and _ (but we will not use it) Variable names are case sensitive (y and Y are different variables)
CHAPTER - 3
- Get link
- X
- Other Apps
Which Editor to use for Javascript? Javascript is very simple and it can be written in as simple an editor as Notepad. But you should use the text editor which helps in debugging the code. My favourite editors are SublimeText and Visual Code Editor . You can download it from here https://www.sublimetext.com/ https://code.visualstudio.com/download Javascript First Program Let's start a javascript program with as simple as alert() <script> alert('Hello World'); </script> Output: Output Options : You can see the output either through document.write() or console.log() If you use the document.write() method in javascript, the output will show in the browser window. Generally, if you are working with HTML and JavaScript, document. write() is ideal for user output. But for testing purposes, you can use console.log() to see the output. Let's change the code and see the output Output:
CHAPTER - 2
- Get link
- X
- Other Apps
1. It's inside browser: The Javascript compiler is inside the browser. You don't need to install any extra software. All browsers are free, so you don't have to pay any license fee. So, it's easy to learn and easy to use. Just write with any text editor and it runs on any browser. Also, it's seamlessly embedded with HTML. 2. One of the most popular programming languages in the world: Javascript is one of the popular programming languages. Whether it's a small company or a big one, JavaScript is used in all companies. 3. Javascript Runs everywhere : Javascript is a language that has been considered a client-side programming language for years. But things are not the same as before. Now, Javascript is used as a client-side as well as server-side programming language. Since it works with the browsers, wherever there is a browser, there is javascript. 4. Easy to learn : Compared to other programming languages like C, JAVA, and c++ etc., Javascript is easy to learn. ...
INTRODUCTION CHAPTER - 1
- Get link
- X
- Other Apps
JavaScript is a scripting language that has been developed to make web pages dynamic, interactive, and user-friendly. Javascript was first developed and launched by Netscape Communications Corp. in Netscape Navigator 2 beta (1995). There should be no confusion that Javascript is not the same as Java language (developed in the 1990s at Sun Microsystems). Mocha -> Livescript -> javascript JavaScript is an object-based language(Java is a fully object-oriented programming language). Javascript offers several built-in objects, and web developers can create or delete their own objects. Though javascript is not completely object-oriented prototypal inheritance makes JavaScript very different from other popular programming languages such as C++, C#, or Java which has features like classes and class inheritance In JavaScript, objects can inherit properties directly from each other, and this way it builds object prototype chains. JavaScript is an interpreted language. ...