KEMBAR78
advancing in js Scripting languages pt4.pptx
Scripting languages
Brian kathabasya
JavaScript String
 The JavaScript string is an object that represents a sequence of
characters.
 There are 2 ways to create string in JavaScript
1. By string literal
2. By string object (using new keyword)
By string literal
 The string literal is created using double quotes
1. <script>
2. var str="This is string literal";
3. document.write(str);
4. </script>
By string object (using new keyword)
1. Here, new keyword is used to create instance of string.
2. <script>
3. var stringname=new String("hello javascript string");
4. document.write(stringname);
5. </script>
JavaScript String charAt(index) Method
 The JavaScript String charAt() method returns the character at the given
index.
1. <script>
2. var str="javascript";
3. document.write(str.charAt(2));
4. </script>
JavaScript String concat(str) Method
1. The JavaScript String concat(str) method concatenates or joins two strings.
2. <script>
3. var s1="javascript ";
4. var s2="concat example";
5. var s3=s1.concat(s2);
6. document.write(s3);
7. </script>
JavaScript String indexOf(str) Method
1. <script>
2. var s1="javascript from javatpoint indexof";
3. var n=s1.indexOf("from");
4. document.write(n);
5. </script>
JavaScript String lastIndexOf(str)
Method
 The JavaScript String lastIndexOf(str) method returns the last index
position of the given string.
1. <script>
2. var s1="javascript from javatpoint indexof";
3. var n=s1.lastIndexOf("java");
4. document.write(n);
5. </script>
JavaScript String toLowerCase()
Method
1. The JavaScript String toLowerCase() method returns the given string in
lowercase letters.
2. <script>
3. var s1="JavaScript toLowerCase Example";
4. var s2=s1.toLowerCase();
5. document.write(s2);
6. </script>
JavaScript String toUpperCase()
Method
 The JavaScript String toUpperCase() method returns the given string in
uppercase letters.
1. <script>
2. var s1="JavaScript toUpperCase Example";
3. var s2=s1.toUpperCase();
4. document.write(s2);
5. </script>
JavaScript String slice(beginIndex, endIndex) Method
 The JavaScript String slice(beginIndex, endIndex) method returns the parts
of string from given beginIndex to endIndex.
 In slice() method, beginIndex is inclusive and endIndex is exclusive.
1. <script>
2. var s1="abcdefgh";
3. var s2=s1.slice(2,5);
4. document.write(s2);
5. </script>
JavaScript String trim() Method
1. <script>
2. var s1=" javascript trim ";
3. var s2=s1.trim();
4. document.write(s2);
5. </script>
JavaScript String split() Method
1. <script>
2. var str="This is JavaTpoint website";
3. document.write(str.split(" ")); //splits the given string.
4. </script>
JavaScript Date Object
 The JavaScript date object can be used to get year, month and day. You
can display a timer on the webpage by the help of JavaScript date object.
 You can use different Date constructors to create date object. It provides
methods to get and set day, month, year, hour, minute and seconds.
 Constructor
 You can use 4 variant of Date constructor to create date object.
1. Date()
2. Date(milliseconds)
3. Date(dateString)
4. Date(year, month, day, hours, minutes, seconds, milliseconds
JavaScript Date Methods
1. Current Date and Time: <span id="txt"></span>
2. <script>
3. var today=new Date();
4. document.getElementById('txt').innerHTML=today;
5. </script>
JavaScript Date Methods
1. <script>
2. var date=new Date();
3. var day=date.getDate();
4. var month=date.getMonth()+1;
5. var year=date.getFullYear();
6. document.write("<br>Date is: "+day+"/"+month+"/"+year);
7. </script>
JavaScript Current Time Example
1. Current Time: <span id="txt"></span>
2. <script>
3. var today=new Date();
4. var h=today.getHours();
5. var m=today.getMinutes();
6. var s=today.getSeconds();
7. document.getElementById('txt').innerHTML=h+":"+m+":"+s;
8. </script>

advancing in js Scripting languages pt4.pptx

  • 1.
  • 2.
    JavaScript String  TheJavaScript string is an object that represents a sequence of characters.  There are 2 ways to create string in JavaScript 1. By string literal 2. By string object (using new keyword)
  • 3.
    By string literal The string literal is created using double quotes 1. <script> 2. var str="This is string literal"; 3. document.write(str); 4. </script>
  • 4.
    By string object(using new keyword) 1. Here, new keyword is used to create instance of string. 2. <script> 3. var stringname=new String("hello javascript string"); 4. document.write(stringname); 5. </script>
  • 5.
    JavaScript String charAt(index)Method  The JavaScript String charAt() method returns the character at the given index. 1. <script> 2. var str="javascript"; 3. document.write(str.charAt(2)); 4. </script>
  • 6.
    JavaScript String concat(str)Method 1. The JavaScript String concat(str) method concatenates or joins two strings. 2. <script> 3. var s1="javascript "; 4. var s2="concat example"; 5. var s3=s1.concat(s2); 6. document.write(s3); 7. </script>
  • 7.
    JavaScript String indexOf(str)Method 1. <script> 2. var s1="javascript from javatpoint indexof"; 3. var n=s1.indexOf("from"); 4. document.write(n); 5. </script>
  • 8.
    JavaScript String lastIndexOf(str) Method The JavaScript String lastIndexOf(str) method returns the last index position of the given string. 1. <script> 2. var s1="javascript from javatpoint indexof"; 3. var n=s1.lastIndexOf("java"); 4. document.write(n); 5. </script>
  • 9.
    JavaScript String toLowerCase() Method 1.The JavaScript String toLowerCase() method returns the given string in lowercase letters. 2. <script> 3. var s1="JavaScript toLowerCase Example"; 4. var s2=s1.toLowerCase(); 5. document.write(s2); 6. </script>
  • 10.
    JavaScript String toUpperCase() Method The JavaScript String toUpperCase() method returns the given string in uppercase letters. 1. <script> 2. var s1="JavaScript toUpperCase Example"; 3. var s2=s1.toUpperCase(); 4. document.write(s2); 5. </script>
  • 11.
    JavaScript String slice(beginIndex,endIndex) Method  The JavaScript String slice(beginIndex, endIndex) method returns the parts of string from given beginIndex to endIndex.  In slice() method, beginIndex is inclusive and endIndex is exclusive. 1. <script> 2. var s1="abcdefgh"; 3. var s2=s1.slice(2,5); 4. document.write(s2); 5. </script>
  • 12.
    JavaScript String trim()Method 1. <script> 2. var s1=" javascript trim "; 3. var s2=s1.trim(); 4. document.write(s2); 5. </script>
  • 13.
    JavaScript String split()Method 1. <script> 2. var str="This is JavaTpoint website"; 3. document.write(str.split(" ")); //splits the given string. 4. </script>
  • 14.
    JavaScript Date Object The JavaScript date object can be used to get year, month and day. You can display a timer on the webpage by the help of JavaScript date object.  You can use different Date constructors to create date object. It provides methods to get and set day, month, year, hour, minute and seconds.  Constructor  You can use 4 variant of Date constructor to create date object. 1. Date() 2. Date(milliseconds) 3. Date(dateString) 4. Date(year, month, day, hours, minutes, seconds, milliseconds
  • 15.
    JavaScript Date Methods 1.Current Date and Time: <span id="txt"></span> 2. <script> 3. var today=new Date(); 4. document.getElementById('txt').innerHTML=today; 5. </script>
  • 16.
    JavaScript Date Methods 1.<script> 2. var date=new Date(); 3. var day=date.getDate(); 4. var month=date.getMonth()+1; 5. var year=date.getFullYear(); 6. document.write("<br>Date is: "+day+"/"+month+"/"+year); 7. </script>
  • 17.
    JavaScript Current TimeExample 1. Current Time: <span id="txt"></span> 2. <script> 3. var today=new Date(); 4. var h=today.getHours(); 5. var m=today.getMinutes(); 6. var s=today.getSeconds(); 7. document.getElementById('txt').innerHTML=h+":"+m+":"+s; 8. </script>