1 JS Intro Opr DT Var (Unit 3)
1 JS Intro Opr DT Var (Unit 3)
KCS-602
by
❑ Operators
❑ Datatypes
❑ Variables
INTRODUCTION TO JAVASCRIPT
• JavaScript is not Java, or even related to Java
• The original name for JavaScript was “LiveScript”
• The name was changed when Java became popular
• Now that Microsoft no longer likes Java, its name for their JavaScript dialect is “Active Script”
<html>
<body>
To Put a JavaScript Into an HTML Page:
<script type="text/javascript">
document.write("Hello World!")
</script>
</body>
</html>
(i) Inline JavaScript
• When java script is written within the html element using attributes related to
events of the element then it is called as inline java script.
Output:
(ii) Internal JavaScript
Java script when written within the section using element is called as internal java script
<html>
<head>
<script>
function msg()
Example: {
alert("Welcome in JavaScript"); }
</script>
</head>
<form>
<input type="button" value="Click" onclick="msg()"/>
</form>
</html>
Output:
click
(iii) External JavaScript
• Writing javascript is written in a separate file with extension .js is called as external java script.
• For adding the reference of an external java script file to html page, use tag with src attribute.
Example: <script type="text/javascript" src="filename.js"/>
• Now, create a file with name message.js and write the following java script functions in it.
function msg()
{ alert("Welcome in JavaScript"); }
<html> <head>
Example: <script type="text/javascript" src="message.js">
</script> </head>
<body>
<form>
<input type="button" value="click" onclick="msg()"/>
</form> </body> </html>
output:
Simple User Interaction: alert(), confirm(), and prompt()
There are three built-in methods of doing simple user interaction
(ii) confirm(msg) Display a message in a dialog box with two buttons: "OK" or "Cancel".
(iii) prompt(msg, default) Display a message and allow the user to enter a value.
• The second argument is the "default value" to be displayed in the input textfield.
• Without the default value, "undefined" is shown in the input textfield.
• If the user click the "OK" button, prompt() returns the value in the input textfield
as a string.
• If the user click the "Cancel" button, prompt() returns null.
alert(), confirm(), and prompt()
JS COMMENTS
There are two types of comments in JavaScript:
1) Single-line Comment: It is represented by double forward slashes (//).
It can be used before and after the statement.
<script>
Ex: // It is single line comment
document.write("hello javascript");
</script>
2) Multi-line Comment: It can be used to add single as well as multi line comments.
It is represented : /* your code here */
<script>
/* It is multi line comment.
Ex: It will not be displayed */
document.write("example of javascript multiline
comment");
</script>
IDENTIFIER
• Contains only 'A' – 'Z', 'a' – 'z', '0' – '9', '_', '$'
• First character cannot be a digit
• Case-sensitive
• Cannot be reserved words or keywords
VARIABLE
It is simply a name of storage location. There are two types of variables in JavaScript :
-local variable and global variable.
<script> Output: 30
Example: var x = 10;
var y = 20;
var z=x+y;
document.write(z);
</script>
<script> <script>
function abc() If(10<13)
Example: { Or,
{
var x=10; //local variable var y=20; // local variable
} }
</script> </script>
(2) global variable
Declared outside the function or declared with window object. It is accessible from any function.
<script>
var data=200; //gloabal variable
function a(){
document.writeln(data); }
Returns a string which can be "number", "string", "boolean", "object", "function", "undefined“ & "null"
<SCRIPT>
var x = "hello";
var y=10;
Example: alert("Variable x value is " + typeof x );
alert("Variable y value is " + typeof y );
alert("Variable z value is " + typeof z );
</script>
OP:
(2) == vs ===
// Type conversion is performed before comparison
var v1 = ("5" == 5); // true
// No implicit type conversion. True if only if both types & values are equal
✓ Introduction to JS
✓ Operators , Datatypes
✓ Variables
Thank
You