Introduction to JavaScript
– JavaScript is a high level, untyped and interpreted programming language. It is programming lang. for HTML & the web
→ Despite some naming, syntactic, and standard library similarities, JavaScript and Java are otherwise unrelated. (it is light weight, uses less resource)
History of JavaScript:
JavaScript was created in todays in May 1995 by Brendan Eich, then working at Netscape and now of Mozilla.
→The original Name of JavaScript was Mocha (name chosen by founder of Netscape).
→ In Sept 1995, name changed to Live Script.
→Again in December 1995, name changed to JavaScript.
Why JavaScript:
-JavaScript is used in web pages to add functionality, validate forms, communicating with server and read write html elements.
→ JavaScript is a client side scripting language.
JavaScript Interpreter:
As the browser goes through the JavaScript it passes it to special program called interpreter, which converts the Java. Script to the machine code your computer understands.
Browser come with JavaScript interpreters.
Where to write code?
→ In the same HTML file 3
In a separate tile, which can be embedded into HTML file.
in HTML File
<script type=”text/JavaScript”>
………………………………………………..
………………………………………………..
</script>
→External file no script tag required.
→linking JavaScript (.js) fide to html file (.html)
<script type=”text/JavaScript” src= “JI.js”>
</script>
JavaScript code in HTML file:
JavaScript code must be inserted between <script> and </script> tags.
→ Script head). can be placed anywhere. in HTML document (body or
→ <script type=”text/JavaScript”> </script>
→The type attributes is not required. JavaScript is the default scripting lang. in HTML.
Example:
<html>
<head>
<script>
document.write (“Hello”);
</script>
</head>
<body>
<H1> Heading </H1>
</body>
</html>
Where can write IS:
- In head tag
- In body tag
- In external file
Atlatching external IS file to html file
<script src= “J.JS”></script>
JavaScript: It is a programming language.
→ Java Script statements are separated by semicolon.
JavaScript statements are composed of: values, operators, Expressions, keywords and comments.
JS Literals: Literals are constants. (Absolutely constant)
→34.25 number (It is diff from variable)
→304 number
→ “Hello” String
Js Variable: Variables are used to store data values.
The var keyword is used to declare variables.
var x=5; //blank variable if there is no assign a value.
Var x;
var x x=x+5 // x = Non (Not a Number)
x=undefined. (Variables are dynamic in nature)
Js Operators: Operators are used to manipulate data and gives a data as result.
Js Expression: An expression is a combination of values, variables, and operators, which computes to a value.
x = 3+4*5; Y=”x=”+x”;
JS Keyword: var is a keyword. As of now there are 63 keywords in JS.
Js Comment: code after double slashes // or between /* and */ i’s treated as a comment. Comments are ignored and will not be executed.
Identifiers Naming Rules: In JS, the first character must be a letter, an underscore (-), or a dollar sign ($). Subsequent characters may be letters, digits, underscores, or dollar sign.
var x=”2″; //string
var y=2 // integer
x==y true {If cheeks only face value}
x===y false { If checks types and face value both}
Popup Boxes: JavaScript has 3 kinds of popup boxes.
- Alert Box
- Confirm Box
- Prompt Box
- Alert Box: Alert is a function in java script.
→In which, we pass a message as an argument, which we can see in alert box.
→When an alert box popup; the user will have to click ok to proceed.
→ Alert is a member function window object.
→alert (“Some message”).
- Confirm box: when a confirm box pops up, the user will have to click either “ok” or “cancel” to proceed.
→If user clicks “ok” the box returns true, if the user clicks “Cancel”, the box return false. →Confirm (“some message”).
Often used if we want the user to verify or accept something.
- Prompt box: A prompt box is often used if you want the user to input a value before entering a page.
→When a prompt box popup, the either “Ok” or value user will have to click “Cancel” to proceed offer entering an input value.
→If the users value, clicks “Ok” the box returns the input If the user clicks “Cancel” the box returns null.
→”Prompt (“message, “default value”);
Representing html element → tag name <p> —</p>___p
By id => <p id=”p1”> — </p>___p1
By class => <p class=”c1”>– </p>___c1
By name <input name=”user”>___user
(In form element)
getElementById() return an object (because i’d is unique)
getElementsByTagName() → it will not return object
(it return arrays of objet)
more than one paragraph.