JAVASCRIPT | WHAT IS JAVASCRIPT | JAVASCRIPT BASICS

Share the post

What is JavaScript?

Now in this topic, We will see What is JavaScript? How the JavaScript code included in the HTML file? How to retrieve the values of HTML input fields? What is a literal function in JavaScript & What are the variables in JavaScript ? JavaScript is the most popular programming language in the web world.
JavaScript is an open-source (Free to use) technology which use to develop the operating functionality of web pages. You don’t need to download any special software to run JavaScript code. On almost any browser you can able to run the JavaScript code.
JavaScript code basically used to give operational functionality to your HTML code. For example, If you have a button on your HTML page then you need to have added an event function for that button. So, you can add this event function to the JavaScript page.

There are basically two types in which you can implement javascript code:

  1. By writing <script></script> tags
  2. By writing a separate js file & give the reference to the HTML page
We will see both the steps :

1. By writing <script></script> tags :

On Same page of HTML you can write under <head> tag you can add <script> tag.You can save the page with .html extension.
<html>
<head>
<script>
 
function onSubmit(){
console.log(“Hello this is the javascript function !!!”);
}
 
</script>
</head>
<body>
  <input type="button" onclick="onSubmit()" />
</body>
 
</html>

2.By writing separate js file & give the reference to HTML page :

On yout .html file write below code
<html>

<head>
 
<script src=”give the full path for your .js file upto .js”></script>
 
</head>
<body>
 <input type="button" onclick="onSubmit()" />
</body>
</html>
On your .js file write the below code
function onSubmit(){

console.log(“Hello this is the javascript function !!!”);
 }
Remember that you don’t need to add <script> tag again on separate .js file.you can reference this js page from any HTML file on your project.
The main advantage of this type is you don’t need to write the same code again and again. You simply create one js file write your common function
call from any HTML file & done.
Now, we will see some basic functionalities of JavaScript.

* How to get the value of the input field by using JavaScript?

=> Suppose you are getting user’s email and password on sign in form.
then you write two input fields on HTML file and to get those values from HTML you can use js function as shown below :
<html>
 
<head>
 
<script>


function onSubmit(){

  var username =  document.getElementById("username").value;
alert(username);


var password = document.getElementById(“password”).value;
  alert(password);

}
 
</script>
 
<title>Sign In Form</title>
</head>
 
<body>

  <input type ="text" id="username" placeholder="Enter username here"/>
  <input type =”password” id=”password” placeholder=”Enter password here”/>
  <input type=”button” onclick=”onSubmit()” />
</body>
 
</html>
In this example, you can find the value of the input field by using the ID.
Similarly, you can get the values by using the class name.

* What is JavaScript literal function?

The literal values of javascript are fixed values & the literal function returns the fixed value.
For example :
<html>

<head>
 
<script>
function onSubmit(x,y){
 
return x + y;
}
 
</script>
 
        <title>Sign In Form</title>
 
</head>
 
<body>

  <input type="button" onclick="onSubmit(1,2)" />
</body>
 
</html>

* What is the variable in JavaScript ?

=> use to store values in javascript. These values are not constant. It’s variable by mean of its name.
For Example :
var x;
x = 1;
This x value is not constant throughout the js.
For Example :
var x;
 
x = 1; ==> NUMBER
x = “Hello”; ==> STRING
In above example x can be defined as a number with value 1;
after that x defined as a string and it’s value also getting change.So, hence proved that variable values are not a constant.

Leave a Comment