JavaScript output
If you want to access an HTML element from JavaScript, you can use the document.getElementById (ID) method.
Use the ID property to identify the HTML element:
JavaScript statementsJavaScript is case sensitive.JavaScript is sensitive to capitalization.
When writing JavaScript statements, be aware of whether to turn off the case toggle key.
The function getElementById is different from the getElementById.
Similarly, variable myvariable and myvariable are different.
JavaScript commentsConsistent with C language:///**/
JavaScript variablesLike algebra, JavaScript variables can be used to hold values (such as x=2) and expressions (such as z=x+y).
Variables can use short names (such as x and y), or they can use better descriptive names (such as age, Sum, totalvolume).
- Variables must start with a letter
- Variables can also start with the $ and _ symbols (although we do not recommend this)
- Variable names are case sensitive (Y and y are different variables)
var pi=3.14; var name= "Bill Gates"; var answer= ' Yes I am! ';
JavaScript Data typesJavaScript has a dynamic type. This means that the same variables can be used for different types:
Object properties are addressed in two ways:
InstanceName=person.lastname;name=person["LastName"];
When you declare a new variable, you can use the keyword "new" to declare its type: var carname=new String;
var x= new number ; var y= new Boolean; var cars= new Array; var New Object;
JavaScript variables are objects. When you declare a variable, a new object is created.
JavaScript Object access properties of an objectThe syntax for accessing object properties is:
objectName. PropertyName
The length property of the string object to find the lengths of the strings
The toUpperCase () method of the String object to convert the text to uppercase
Creating JavaScript ObjectsWith JavaScript, you can define and create your own objects.
There are two different ways to create a new object:
- Defining and creating an instance of an object
- Use a function to define an object, and then create a new object instance
Create a direct instanceThis example creates a new instance of the object and adds four properties to it: instance
person=New Object ();p erson.firstname= "Bill";p erson.lastname= "Gates";p erson.age =56;p erson.eyecolor= "Blue";
Using the Object BuilderThis example uses a function to construct an object:
Instancefunction Person (Firstname,lastname,age,eyecolor) {this. firstname=FirstName; this. lastname=LastName; this. age= age; this. eyecolor=Eyecolor;}
Creating an instance of a JavaScript objectOnce you have the object constructor, you can create a new object instance, just like this:
var myfather=New person ("Bill", "Gates", "page", "Blue"); var mymother=New person ("Steve", "Jobs", "green");
JavaScript functionsA function is a block of code wrapped in curly braces, preceded by a keyword function
You can send any number of arguments separated by commas (,):
MyFunction (argument1,argument2)
When you declare a function, declare the argument as a variable:
function myFunction (VAR1,VAR2) {This is the code to execute}
Variables and parameters must appear in a consistent order. The first variable is the given value of the first parameter passed, and so on.
Assigning a value to an undeclared JavaScript variableIf you assign a value to a variable that has not been declared, the variable is automatically declared as a global variable.
JavaScript classJavaScript is an object-oriented language, but JavaScript does not use classes.
In JavaScript, classes are not created and objects are not created through classes (as in other object-oriented languages).
JavaScript is based on prototype, not class-based.
Comparison operatorsComparison operators are used in logical statements to determine whether a variable or value is equal. Other same C
Operator |
Describe |
Example |
=== |
Congruent (value and type) |
X===5 is true;x=== "5" is false |
For/in Cycle with OCThe JavaScript for/in statement loops through the properties of the object:
Instancevar person={fname: "John", lname: "Doe", age:25 for on person ) { txt =txt + person[x]; }
Break statement is used to jump out of a loop.
Continue used to skip an iteration in the loop.
JavaScript errors-Throw, Try, and CatchThe try statement tests the code block for errors.
The catch statement handles the error.
The throw statement creates a custom error.
JavaScript Testing and capturingThe try statement allows us to define a block of code that performs error testing at execution time.
The catch statement allows us to define a block of code that executes when a try block of code has an error.
JavaScript statements try and catch are paired occurrences
Throw statementThe throw statement allows us to create custom errors.
The correct technical term is: Create or throw an exception (exception).
If you use throw with try and catch, you can control the flow and generate custom error messages.
Try { varX=document.getelementbyid ("Demo"). Value; if(x== "")Throw"Empty"; if(IsNaN (x))Throw"Not a number"; if(X>10)Throw"Too High"; if(x<5)Throw"Too Low"; }Catch(err) {varY=document.getelementbyid ("mess"); Y.innerhtml= "Error:" + Err + "."; }
JavaScript form validation Cue box functionAlert (Alerttxt)
More properties, functions, methods, etc., see the website brochure
JavaScript's personal learning is a handy note (i)