Words, Numbers, and Dates
JavaScript made up of Objects.
Properties - parts of the object.
Methods - actions and object perform.
Object - array
Properties - length
Method - push()
Object - document
Method - write
Whenever you create a new variable and store a value into it, you are creating an instance.
Strings
Length
to determine the length of a string:
name.length
var password = 'sesame';
if (password.length <= 6) {
alert('That password is too short.');
} else if (password.length > 15) {
alert('That password is too long.');
}
Chang the Case of a String
name.toUpperCase
name.toLowerCase
The correct answer to a quiz is LeCuyer, but the person types Lecuyer so they get it wrong.
To correct you can covert both the response and the correct answer to uppercase
if (response.toUpperCase() == correctAnswer.toUpperCase()) {
//correct
} else {
//incorrect
}
Covert to lowercase:
var answer= 'LeCuyer';
alert(answer.toLowerCase());
returns : lecuyer
In both cases the original string is not altered, it is still LeCuyer.
Searching a String: indexOf()
To find out which browser the viewer is using:
Navigator is an object of the web browser, userAgent is the property
<script type="text/javascript">
alert(navigator.userAgent);
</script>
indexOf()
if the search string isn't found the method returns: -1
var browser = navigator.userAgent; // this is a string
if (browser.indexOf('MSIE') != -1) {
// this is Internet Explorer
}
if the searched string is found it returns the position number.
(spaces count as a position: poop face has 0-8
var myString = "Poop Face";
var firstPosition = myString.indexOf('Poo'); // returns 0
var lastPosition = myString.lastIndexOf('Face'); // returns 5
------
Slice
myString.slice(7);
This picks the 6th position of the string.
myString.slice(start,end);
The end is actually the last position +1.
so (0,5) selects the first five letters/positions 0-6
you can also use negative numbers
Regular Expressions
var myMatch = /hello/;
this matches the word hello
How to search using the search() Method
var myRegEx = /To be/;
var quote = 'To be or not to be.';
var foundPosition = quote.search(myRegEx); //returns 0, it returns the first position in the match
No comments:
Post a Comment