Study JavaScript Easy Part-3
Previous
JavaScript study Continued….
Special Symbols in JavaScript
A backslash ( \ ) is used to add special symbols or characters in a javascript.The special symbols are quotes,newlines,tabs etc etc.
Eg:1(check difference between both codes)
document.write(“welcome to “ooty” nice to meet you”);
document.write(“welcome to\”ooty\” nice to meet you”);
Given below is a list of special characters that can be added to a text string with the help of a backslash sign.
\’ Single quote
\” Double quote
\n New line
\b BackSpace
\& Ampersand
JavaScript In Depth
Believe it or not JS is an object oriented language.It allows our own objects and variable types.
What is an object??
An object is a special kind of data,it has properties and methods.Length is the property of a string.Methods are the actions that can be performed on objects.To change the size of a string,change its case etc etc are methods.
I am writing down a few Eg:,which i think would help in one way or the other.
Eg:2(To find the length of a string)
<html>
<body>
<script type=”text/javascript”>
var txt=”lloyd”;
document.write(txt.length);
document.write(“<p>Lowercase: ” + txt.toLowerCase() + “</p>”);
document.write(“<p>Uppercase: ” + txt.toUpperCase() + “</p>”);
document.write(“<p>Fontcolor: ” + txt.fontcolor(“Blue”) + “</p>”);
</script>
</body>
</html>
Result is:
5
Lowercase: lloyd
Uppercase: LLOYD
Fontcolor: lloyd
Eg:3(indexOf( ):-To find the position of a word)
<html>
<body>
<script type=”text/javascript”>
var str=”india is mine”;
document.write(str.indexOf(“india”) + “<br />”);
document.write(str.indexOf(“is”) + “<br />”);
document.write(str.indexOf(“mine”) + “<br />”);
document.write(str.indexOf(“how”));
</script>
</body>
</html>
Result:
0
6
9
-1
Note:The -1 in the result indicates that no such word in the string.
Eg:4(replace( ):-To replace characters in a string)
<html>
<body>
<script type=”text/javascript”>
var str=”Visit Us”;
document.write(str.replace(/Us/,”http:\\\\www.three2tango.com”));
</script>
</body>
</html>
The use is same as normal arrays in C,C++.It is used to store multiple values in a single variable.I will come up with Eg: so that it is easily understood.
Eg:5(How to create an array)
<html>
<body>
<script type=”text/javascript”>
var mypassion = new Array();
mypassion[0] = “Dot net”;
mypassion[1] = “C#”;
mypassion[2] = “sql”;
for (i=0;i<mypassion.length;i++)
{
document.write(mypassion[i] + “<br />”);
}
</script>
</body>
</html>
Eg:6(Concatinating arrays)
<html>
<body>
<script type=”text/javascript”>
var t = new Array(3);
t[0] = “I”;
t[1] = “Love”;
t[2] = “You”;
var s = new Array(3);
s[0] = “Because”;
s[1] = “You are”;
s[2] = “Good”;
document.write(t.concat(s));
</script>
</body>
</html>
Result :
I,Love,You,Because,You are,Good
Note:In the above Eg: t,s are 2 arrays of size 3 each,they are concatenated using the method .”concat( )”.
Eg:7(To sort an array)
<html>
<body>
<script type=”text/javascript”>
var arr = new Array(6);
arr[0] = “vivek<br/>”;
arr[1] = “dileep<br/>”;
arr[2] = “shefeek<br/>”;
arr[3] = “hari<br/>”;
arr[4] = “arun<br/>”;
arr[5] = “joseph<br/>”;
document.write(arr + “<br />” );
document.write(“sorted list<br/>”)
document.write(arr.sort());
</script>
</body>
</html>
Result:
vivek
,dileep
,shefeek
,hari
,arun
,joseph
sorted list
arun
,dileep
,hari
,joseph
,shefeek
,vivek
Note:The method used to sort an array is shown above,it will sort a string array in alphabetical order.
Goto Prev
Related posts:








Leave your response!