Underneath its simplicity that deceives many are some of the most powerful features, which are even now used by an absurd number of high-end applications, showing that a deep knowledge of this language is one of the most important skills for today’s Web developer .
And the other difference is that functions are objects, giving functions an incredible ability to store executable code and be passed on as a parameter to any other object.
You can use your tag within the <head> tag or within the <body> tag.
A small sample of code for you to better understand what we’re talking about. Suppose you want to display the current date (in the format dd-mm-yyyy, that is, day-month-year) on your website. Then you will need to create an HTML document with the following code:
<script> var today = new Date ( ) ; var dd = today. getDate ( ) ; var mm = today. getMonth ( ) + 1 ; var yyyy = today. getFullYear ( ) ; if ( dd < 10 ) dd = '0' + dd ; if ( mm < 10 ) mm = '0' + mm ; today = dd + '-' + mm + '-' + yyyy ; document. write ( "<strong>" + today + "</strong>" ) ; </script>
<! DOCTYPE html> <html> <head> <title> </title> <meta charset = "utf-8" /> <script src="currentdate.js"></script> </head> <body> </body> </html>
Save this HTML document as index.html. Make sure that you have saved both index.html and current.js in the same folder. If they are in separate folders, it means that when you set the value of the src attribute of the <script> tag, you must specify the complete and correct path to the js file.
Now if you open the index.html file using your web browser, you will have a web page that will display the current date. Pretty good huh?
However, if you have many lines of code that are very common for many of your Web pages, then you should consider keeping your code externally.
Well, you will have to make changes to just a single .js file, and that change will be reflected on all web pages that have the link to the file.
You can also use variables in expressions to perform various types of calculations. We will assign unique names for each variable. Although you are free to place any names you want in each variable. But, although variable names can contain letters, numbers, dollar signs and underscores, variable names must never start with a digit.
List of valid and other invalid variables:
your name $ developer employee_name _my store
programmers let hired-employee # games developer student danki
Now let’s see how you can create your own variables. Before you can use a variable, you must create it, that is, declare it. You must use the var keyword to declare a variable, as in the example below:
var developerWeb = 10;
You can very well assign a value to the variable as soon as you declare it. That is, the following code snippets are both correct:
var web developer; web developer = 10;
ORweb developer var = 10;
In a variable, not only numbers are stored, but strings can also be stored. Example: (“John”, “My name is John”);
Expressions: (8 + 10);
All of these different types of data that variables can contain are known as data types. When you assign numbers, expressions or Boolean values, you enclose the value in double quotes. String values, on the other hand, need to be enclosed in double quotes. Example:
var companyName = “Danki Code”; var isSubmitted = true; var sumVal = 8 + 10;
You can easily concatenate string values using the + operator. The only thing you need to do is add a plus sign between the strings, and that’s it, it’s concatenated. Example:
var firstName = “John”; var aboutName = “Smith”; var fullName = firstName + ”“ + aboutName;
Now, what is the value of the variable fullName? You got it right if you said “John Smith”. Note that an empty space has been added between the two names.
Sometimes you will have to collect user input data and display it on your website after adding some messages. So, how can you get this user input data? Very simple. You will need to use the prompt () method to collect this data. The prompt () method displays a dialog box (with a text box for the user to enter the input data) and two more buttons: OK and CANCEL. After placing an entry, the user has to click OK or CANCEL to continue.
What we are going to do now is to directly assign the prompt () method to a variable so that the input will be immediately assigned to the variable as soon as the user enters any input. The prompt () syntax is:
prompt (message, text) where ‘message’ is the message that is displayed in the dialog box and text is the default text that is displayed in the text box. Message is a mandatory parameter whereas text is an optional parameter. Example:
var usernameName = prompt ("What is your name?", "Type here");
There is another method for displaying the data in the output of the web page. The method is document.write () to print something on your page.
The syntax for the document.write () method is: document.write (exp1, exp2,…). Where exp1, exp2, etc. are expressions to print a webpage. And you can have as many expressions as you like. Example:
document.write (“My name is Fábio”); var firstName = "Guilherme"; document.write (firstName);
Sometimes your code will not be updated or maintained by you, but by someone else, and they may not understand the reason for certain lines of code. Thus, adding comments is an excellent programming practice.