Monday, October 31, 2022

JavaScript

 JavaScript(JS)

JavaScript scripting language , which is used to enhance the functionality and appearance of web pages.

All web browser have JavaScript interpreters.

JavaScript is a browser-based Scripting language developed by Netscape and implemented in all major browsers.

Scripts can be Static or Dynamic.

JavaScript is Case Sensitive.

JS Memory Concepts - similar to Java/C.


Sample Program

You want to open Notepad , it is a default software in your system. After typing the code , you want to save the file as ".html" (eg: 1.html like this you want to save your file).
<!DOCTYPE html>
<html>
<head>
<meta charset =''utf-8">
<title>A first program in JavaScript</title>
<script type="text/javascript">
  document.writeln("<h1> Welcome to JavaScript Programming!</h1>");
</script>
</head>
<body></body>
</html>

Result
You can view your result in the web browser like Internet explorer , Chrome , Firefox etc.,.
Sample Program
JavaScript Program


Escape Sequences in JavaScript
  • \n - New Line - Position the screen cursor at the beginning of the next line.
  • \t -  Horizontal Tab - Move the screen cursor to the next line.
  • \\ - Backslash - Used to represent a backslash character in a string.
  • \" - Double quote - Used to represent  a double-quote character in a string contained in double quotes.                                      For Example : window.alert("\" in double quotes\"");          Displays "in double quotes" in  an alert dialog.
  • \' -  Single quote - Used to represent  a Single-quote character in a string.                                                                                      For Example : window.alert("\" in single quotes\"");      Displays "in Single quotes" in  an alert dialog.
  • // -  Forward slash - Single line Comment that begins with the character // states the purpose of the variable in the script.       For Example : // read the name from the prompt box as a string                                                                                               name= window.prompt("Please enter your name");                  // covert numbers from strings to integers                                number1= parseInt(firstnumber);
  • /*....*/ -  Backslash and star - Multiline Comment - It can be split over many lines.                                                                             For Example : /* It can be split over many lines. */
Data Types of  JavaScript

Loosely Defined Data types- Can hold any type of data.

  • Arithmetic - Addition(+) , Subtraction(-) ,Multiplication(*) ,Division( /) ,Remainder (%).
  • Decision making (if statement) : Equality operators - Equal(==) , Not Equal(!=).                                             Relational Operator - Greater than(>) , Lesser than(<) , Greater than or equal to (>=) , Lesser than or equal to(<=).
  • Strict Equals(===) and  Strict Does Not Equals(!==) Operators. For Example : comparison "95" === 95 yields the value false because one is string and the other one is number. Similarly 95" !== 95 yields true  because  the operand 's type are not equal ,therefore the values are not equal.


To implement the Function , Arrays and Objects using JavaScript

Format/Syntax:
Function Call:
function functionname(parameter-list)
{
   declarations and statements
}
return statement;
return expression; (or) return;

What is Array in javascript?
In  JavaScript Array is a single variable that is used to store different elements. There are basically two ways to declare an array.
Array is a Dynamic . Size of the array in JavaScript can be changed.
Arrays knows their length(length property).
Arrays in Memory : Objects and Operator or new(Allocates memory for objects and Dynamic memory allocation operator). Eg: var c;
c= new Array(12)
 
Program
  • Open the Notepad(HTML editor)
  • Script must contain in head section
  • Code for Array must be written inside the script.
  • Use the Syntax build the codes for the respective functions.
  • Save the html program using .html extension.
Reversing the Array
<html>
<head>
<meta charset ="utf-8">
<title>Sample Array</title>
<script type="text/javascript">
function start()
{
 var a =[10,20,30,40,50,60,70,80,90,100];
 document.writeln("<h1>Reversing the array</h1>");
 outputArray("Data items in original order:",a);
 a.reverse(swap);
 outputArray("Data items in reverse order:",a);
}
function outputArray(header, theArray)
{
  document.writeln("<p>"+header+theArray.join(" ")+"<p>");
}
function swap()
{
  for(a=0;a>=0;a--)
  {
    return a;
  }
}
</script>
</head>
<body onload="start()"></body>
</html>

Result
You can view your result in the web browser like Internet explorer , Chrome , Firefox etc.,.
Array JavaScript
Array JavaScript


What is Object in javascript?
Objects in javascript may be defined as an unordered collection of related data of primitive or reference types in the form of  "Key: value" pair.
Program
  • Open the Notepad(HTML editor)
  • Script must contain in head section
  • Code for Object must be written inside the script.
  • Use the Syntax build the codes for the respective functions.
  • Save the html program using .html extension.
Object(Click button to view Math.Round function answer)
<html>
<head>
<title>Object</title>
<script type="text/javascript">
function md()
{
 document.getElementById("demo").innerHTML=Math.Round(2.5);
}
</script>
</head>
<body>
<p id="demo"> Click the button</p>
<button onclick="md()">Click</button>
</body>
</html>

Result
You can view your result in the web browser like Internet explorer , Chrome , Firefox etc.,.

Object
Object JavaScript 1.1
Object
Object JavaScript 1. 2


What is Function in javascript?
JavaScript functions are defined with the function keyword. We can use a function declaration or a function expression. In javascript functions are objects and they have both properties and methods.

Program
  • Open the Notepad(HTML editor)
  • Script must contain in head section
  • Code for  Function must be written inside the script.
  • Use the Syntax build the codes for the respective functions.
  • Save the html program using .html extension.
Recursion of Fibonacci Function
<html>
<head>
<title>Recursion of Fibonacci Function</title>
<h1>Recursion of Fibonacci Function for 0 to 10</h1>
<script type="text/javascript">
for(var i=0;i<=10;i++)
{document.write("<br> Fibonacci of " + i +" is "+fibonacci(i)); }
function fibonacci(num) 
   {    
        if(num==0) 
            return 0; 
        if (num == 1) 
            return 1; 
        else
        return  fibonacci(num - 1) + fibonacci(num - 2);
   } 
</script>
</head>
</html>

Result
You can view your result in the web browser like Internet explorer , Chrome , Firefox etc.,.
Fibonacci Function

"JavaScript(JS)" easy for beginners to learn and Understand .  

I hope you will like and enjoy this Blog . If you have any queries you can comment in the comment box .
If you like this blog you can share with your friends.

Thank you  

For spending your valuable time to read my Blog.


Print Friendly and PDF

No comments:

Post a Comment