Every Rockstar programmer started with this same program before moving on to become Legend and create wonders. So even we will start with the same. The basic aim of this exercise is to get familiar with the Syntax and how it all works.
So, first according to Wikipedia :
JavaScript (JS) is a dynamic computer programming language. It is most commonly used as part of web browsers, whose implementations allow client-side scripts to interact with the user, control the browser, communicate asynchronously, and alter the document content that is displayed. It is also being used in server-side network programming (with Node.js), game development and the creation of desktop and mobile applications.Pretty cool! Right?
So, let's fire up the Notepad++ and create a New file and Save as - helloworld.html. I know, you must be thinking that why are we saving it before typing anything. Well remember I told you about how Notepad++ has Syntax color coding in my previous post. So, if we save it as HTML, if will automatically suggest tags as we type. Awesome.
Now let's see the syntax for Hello World program :
<html>
<head />
<body>
<script type="text/javascript">
document.write("Hello World");
</script>
</body>
</html>
Just type it as it is in the Notepad++ and Go to Run--> Launch in Chrome or simply use the shortcut Ctrl+Alt+Shift+R.
Output:
Now let's understand how it works. We basically embedded our Script (Read: JavaScript program) into the HTML page. So that when we run the html page, our Script is automatically executed and show us the message "Hello World".
To write script, we will use this snippet :
<script type="text/javascript">and based on the position in the Body of HTML page, the Script will execute.
document.write("Hello World");
</script>
So everything should be enclosed in <script /> tag.
Syntax Explained:
- document.write( ) : It can be seen as Programming equivalent of "Print" (C, anyone?). So whatever we will write in the ( braces ) will be printed out aka Shown on the webpage. If you are not able to understand. Don't fret it. I will explain it properly in next lesson.
So, the basic aim of this Lesson was to check whether your environment is setup properly to code and compile JavaScript.
That's all for this lesson. See you in the next one.
No comments:
Post a Comment