JAVASCRIPT-DATA-TYPES1-1
JAVASCRIPT-DATA-TYPES1-1
JavaScript variables can- hold many data types: numbers, strings, arrays, objects and more
JavaScript Strings
- It is a series of characters like "John Doe".
- Strings are written with quotes. You can use single or double quotes.
- Ex. var carName = "Volvo XC60";
JavaScript Numbers
- JavaScript has only one type of numbers.
- Numbers can be written with, or without decimals:
- Extra large or extra small numbers can be written with scientific (exponential) notation.
- Ex. var x1 = 34.00;
JavaScript Booleans
- Booleans can only have two values: true or false.
- Booleans are often used in conditional testing.
- Ex. var x = true; var y = false;
JavaScript Arrays
- JavaScript arrays are written with square brackets.
- Array items are separated by commas.
- Ex. var cars = ["Saab", "Volvo", "BMW"];
JavaScript Objects
- JavaScript objects are written with curly braces.
- Object properties are written as name:value pairs, separated by commas.
- Ex. var person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
The typeof Operator
- You can use the JavaScript typeof operator to find the type of a JavaScript variable:
- Ex. typeof "John" // Returns string
Undefined
- In JavaScript, a variable without a value, has the value undefined. The typeof is also undefined.
- Ex. var person;
- Any variable can be emptied, by setting the value to undefined. The type will also be undefined.
Empty Values
- An empty value has nothing to do with undefined.
- An empty string variable has both a value and a type.
- Ex. var car = "";
Null
- In JavaScript null is "nothing". It is supposed to be something that doesn't exist.
- Unfortunately, in JavaScript, the data type of null is an object.
- Ex. var person = null;
JavaScript Variables
- JavaScript variables are containers for storing data values.
JavaScript Identifiers
- All JavaScript variables must be identified with unique names.These unique names are called identifiers.
JavaScript Functions
- A JavaScript function is a block of code designed to perform a particular task.
- Ex. function myFunction(p1, p2) {
return p1 * p2;
}
JavaScript Scope
Scope is the set of variables you have access to.
-
The Number() Method
- Number(), can be used to convert JavaScript variables to numbers
The parseInt() Method
- parseInt() parses a string and returns a whole number. Spaces are allowed.
The parseFloat() Method
- parseFloat() parses a string and returns a number. Spaces are allowed.
JavaScript If...Else Statements
JavaScript HTML DOM
With the HTML DOM, JavaScript can access and change all the elements of an HTML document.
The HTML DOM (Document Object Model)
- When a web page is loaded, the browser creates a Document Object Model of the page.
- The HTML DOM model is constructed as a tree of Objects:
- With the object model, JavaScript gets all the power it needs to create dynamic HTML:
JavaScript can change all the HTML elements in the page
JavaScript can change all the HTML attributes in the page
JavaScript can change all the CSS styles in the page
JavaScript can remove existing HTML elements and attributes
JavaScript can add new HTML elements and attributes
JavaScript can react to all existing HTML events in the page
JavaScript can create new HTML events in the page
What is the HTML DOM?
- The HTML DOM is a standard object model and programming interface for HTML. It defines:
The HTML elements as objects
The properties of all HTML elements
The methods to access all HTML elements
The events for all HTML elements
JavaScript - HTML DOM Methods
- HTML DOM methods are actions you can perform (on HTML Elements).
- HTML DOM properties are values (of HTML Elements) that you can set or change.
The DOM Programming Interface
- In the DOM, all HTML elements are defined as objects.
- The programming interface is the properties and methods of each object.
A property is a value that you can get or set (like changing the content of an HTML
element).
A method is an action you can do (like add or deleting an HTML element).
HTML Forms
The <form> Element
- HTML forms are used to collect user input.
The <input> Element
- The <input> element is the most important form element.
- The <input> element has many variations, depending on the type attribute.
Here are the types used in this chapter:
Text Input
- <input type="text"> defines a one-line input field for text input.
Radio Button Input
- <input type="radio"> defines a radio button.
- Radio buttons let a user select
-
The Submit Button
- <input type="submit"> defines a button for submitting a form to a form-handler.
- The form-handler is typically a server page with a script for processing input data.
The Method Attribute
- The method attribute specifies the HTTP method (GET or POST) to be used when submitting the
forms
When to Use GET?
- if the form submission is passive (like a search engine query), and without sensitive information.
When to Use POST?
- If the form is updating data, or includes sensitive information (password).
HTML Input Attributes
- The value attribute specifies the initial value for an input field:
The readonly Attribute
- The readonly attribute specifies that the input field is read only (cannot be changed):
The disabled Attribute
- A disabled element is un-usable and un-clickable.
The size Attribute
- The size attribute specifies the size (in characters) for the input field
Data Validation
- Data validation is the process of ensuring that computer input is clean, correct, and useful.
- Validation can be defined by many different methods, and deployed in many different ways.
Server side validation is performed by a web server, after input has been sent to the
server.
Client side validation is performed by a web browser, before input is sent to a web
server.
JavaScript Validation API
EXAMPLE:
<html><body>
Input Number:<input id="id1" type="number" max="50">
<button onclick="check()">OK</button>
<p id="demo"></p>
<script>
function check() {
var txt = "";
if (document.getElementById("id1").validity.rangeOverflow) {
txt = "Value too large"; } }
document.getElementById("demo").innerHTML = txt; }
</script> </body></html>