{"id":707,"date":"2010-07-07T01:31:19","date_gmt":"2010-07-07T01:31:19","guid":{"rendered":"http:\/\/www.phpmind.com\/blog\/?p=707"},"modified":"2010-07-17T01:35:43","modified_gmt":"2010-07-17T01:35:43","slug":"javascript-variable-manipulation-functions","status":"publish","type":"post","link":"https:\/\/www.phpmind.com\/blog\/2010\/07\/javascript-variable-manipulation-functions\/","title":{"rendered":"JavaScript Refrence"},"content":{"rendered":"<h2>JavaScript Variable Manipulation Functions<\/h2>\n<p>As shown in the following table, you can use these JavaScript statements in your own code to create and modify variables in your JavaScript functions.<\/p>\n<table border=\"1\" cellspacing=\"2\" cellpadding=\"2\">\n<tbody>\n<tr>\n<th>Element<\/th>\n<th>Description<\/th>\n<\/tr>\n<tr>\n<td><span class=\"code\">var myVar = 0;<\/span><\/td>\n<td>Creates a variable with given starting value. Type is determined dynamically.<\/td>\n<\/tr>\n<tr>\n<td><span class=\"code\">stringVar = prompt(&#8220;message&#8221;)<\/span><\/td>\n<td>Sends message to user in a <a id=\"KonaLink0\" class=\"kLink\" style=\"text-decoration: underline ! important; position: static;\" href=\"#\" target=\"undefined\"><span style=\"color: blue ! important; font-weight: 400; font-size: 12px; position: static;\"><span class=\"kLink\" style=\"color: blue ! important; font-family: arial,verdana,sans-serif; font-weight: 400; font-size: 12px; position: relative;\">dialog <\/span><span class=\"kLink\" style=\"color: blue ! important; font-family: arial,verdana,sans-serif; font-weight: 400; font-size: 12px; position: relative;\">box<\/span><\/span><\/a>, retrieves text input from user and stores it in <span class=\"code\">stringVar<\/span>.<\/td>\n<\/tr>\n<tr>\n<td><span class=\"code\">stringVar.length<\/span><\/td>\n<td>Returns the length (in characters) of <span class=\"code\">stringVar<\/span>.<\/td>\n<\/tr>\n<tr>\n<td><span class=\"code\">stringVar.toUpperCase(), stringVar.toLowerCase()<\/span><\/td>\n<td>Converts <span class=\"code\">stringVar<\/span> to upper- or lowercase.<\/td>\n<\/tr>\n<tr>\n<td><span class=\"code\">stringVar.substring()<\/span><\/td>\n<td>Returns a specified subset of <span class=\"code\">stringVar<\/span>.<\/td>\n<\/tr>\n<tr>\n<td><span class=\"code\">stringVar.indexOf()<\/span><\/td>\n<td>Returns location of a substring in <span class=\"code\">stringVar<\/span> (or <span class=\"code\">-1<\/span>).<\/td>\n<\/tr>\n<tr>\n<td><span class=\"code\">parseInt()<\/span><\/td>\n<td>Converts <span class=\"code\">string<\/span> to <span class=\"code\">int<\/span>.<\/td>\n<\/tr>\n<tr>\n<td><span class=\"code\">parseFloat()<\/span><\/td>\n<td>Converts <span class=\"code\">string<\/span> to <span class=\"code\">float<\/span>.<\/td>\n<\/tr>\n<tr>\n<td><span class=\"code\">toString()<\/span><\/td>\n<td>Converts any variable to <span class=\"code\">string<\/span>.<\/td>\n<\/tr>\n<tr>\n<td><span class=\"code\">eval()<\/span><\/td>\n<td>Evaluates <span class=\"code\">string<\/span> as JavaScript code.<\/td>\n<\/tr>\n<tr>\n<td><span class=\"code\">Math.ceil()<\/span><\/td>\n<td>Converts any number to integer by rounding up.<\/td>\n<\/tr>\n<tr>\n<td><span class=\"code\">Math.floor()<\/span><\/td>\n<td>Converts any number to integer by rounding down.<\/td>\n<\/tr>\n<tr>\n<td><span class=\"code\">Math.round()<\/span><\/td>\n<td>Converts any number to integer by standard rounding algorithm.<\/td>\n<\/tr>\n<tr>\n<td><span class=\"code\">Math.random()<\/span><\/td>\n<td>Returns random float between 0 and 1.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"series-article\">\n<h2>Basic I\/O Commands in JavaScript<\/h2>\n<p>JavaScript programmers commonly use the commands shown in the following table for controlling dialog-based input and output in programs to be used on the Web.<\/p>\n<table border=\"1\">\n<tbody>\n<tr>\n<th>Element<\/th>\n<th>Description<\/th>\n<\/tr>\n<tr>\n<td><span class=\"code\">alert(&#8220;message&#8221;);<\/span><\/td>\n<td>Creates a popup dialog containing &#8220;message.&#8221;<\/td>\n<\/tr>\n<tr>\n<td><span class=\"code\">stringVar = prompt(&#8220;message&#8221;)<\/span><\/td>\n<td>Send message to user in a dialog box, retrieve text input from user and store it in <span class=\"code\">stringVar<\/span>.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<div class=\"series-article\">\n<h2>JavaScript Conditions and Branching Code Structures<\/h2>\n<p>Look to the following table for JavaScript control structures you can use in your program code to add branching and looping behavior to your JavaScript programs.<\/p>\n<table>\n<tbody>\n<tr>\n<th>Element<\/th>\n<th>Description<\/th>\n<\/tr>\n<tr>\n<td><span class=\"code\">if (condition){<\/span><span class=\"code\"><br \/>\n<\/span><\/p>\n<p><span class=\"code\">\/\/ content<\/span><span class=\"code\"> <\/span><\/p>\n<p><span class=\"code\">} else {<\/span><span class=\"code\"> <\/span><\/p>\n<p><span class=\"code\">\/\/ more content<\/span><span class=\"code\"> <\/span><\/p>\n<p><span class=\"code\">} \/\/ end if<\/span><\/td>\n<td>Executes content only if condition is true.<\/p>\n<p>Optional <span class=\"code\">else<\/span> clause occurs if condition<br \/>\nis false.<\/td>\n<\/tr>\n<tr>\n<td><span class=\"code\">switch (expression)<\/span><span class=\"code\"> <\/span><\/p>\n<p><span class=\"code\">case: value;<\/span><span class=\"code\"> <\/span><\/p>\n<p><span class=\"code\">\/\/code<\/span><span class=\"code\"> <\/span><\/p>\n<p><span class=\"code\">break;<\/span><\/p>\n<p><span class=\"code\">default:<\/span><\/p>\n<p><span class=\"code\">\/\/code<\/span><span class=\"code\"><br \/>\n<\/span><span class=\"code\">}<\/span><\/td>\n<td>Compares expression against one or more values. If expression<br \/>\nis equal to value, runs corresponding code.<\/p>\n<p>Default clause catches any uncaught values.<\/td>\n<\/tr>\n<tr>\n<td><span class=\"code\">for(i = 0; i &lt; count; i++)<\/span><\/p>\n<p><span class=\"code\">\/\/code<\/span><\/p>\n<p><span class=\"code\">} \/\/ end for<\/span><\/td>\n<td>Repeats code <span class=\"code\">i<\/span> times.<\/td>\n<\/tr>\n<tr>\n<td><span class=\"code\">While (condition){<\/span><\/p>\n<p><span class=\"code\">\/\/code<\/span><\/p>\n<p><span class=\"code\">} \/\/ end while<\/span><\/td>\n<td>Repeats code as long as condition is true.<\/td>\n<\/tr>\n<tr>\n<td><span class=\"code\">Function fnName(paramaters) {<\/span><\/p>\n<p><span class=\"code\">\/\/code<\/span><\/p>\n<p><span class=\"code\">} \/\/ end function<\/span><\/td>\n<td>Defines a function named <span class=\"code\">fnName<\/span> and<br \/>\nsends it parameters. All code inside the function will execute when<br \/>\nthe function is called.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<div class=\"series-article\">\n<h2>Add JavaScript Comparison Operators to Condition Statements<\/h2>\n<p>JavaScript uses comparison operators inside conditions to make numeric or alphabetical comparisons of variables to other variables or values. Using these operators, you can determine whether a variable is greater than, less than, or equal to another variable or value. You can also use combinations of these comparison operators.<\/p>\n<table>\n<tbody>\n<tr>\n<th>Name<\/th>\n<th>Operator<\/th>\n<th>Example<\/th>\n<th>Notes<\/th>\n<\/tr>\n<tr>\n<td>Equality<\/td>\n<td><span class=\"code\">==<\/span><\/td>\n<td><span class=\"code\">(x==3)<\/span><\/td>\n<td>Works with all variable types, including strings.<\/td>\n<\/tr>\n<tr>\n<td>Not equal<\/td>\n<td><span class=\"code\">!=<\/span><\/td>\n<td><span class=\"code\">(x != 3)<\/span><\/td>\n<td>True if values are not equal.<\/td>\n<\/tr>\n<tr>\n<td>Less than<\/td>\n<td><span class=\"code\">&lt;<\/span><\/td>\n<td><span class=\"code\">(x &lt; 3)<\/span><\/td>\n<td>Numeric or alphabetical comparison.<\/td>\n<\/tr>\n<tr>\n<td>Greater than<\/td>\n<td><span class=\"code\">&gt;<\/span><\/td>\n<td><span class=\"code\">(x &gt; 3)<\/span><\/td>\n<td>Numeric or alphabetical comparison.<\/td>\n<\/tr>\n<tr>\n<td>Less than or equal to<\/td>\n<td><span class=\"code\">&lt;=<\/span><\/td>\n<td><span class=\"code\">(x &lt;= 3)<\/span><\/td>\n<td>Numeric or alphabetical comparison.<\/td>\n<\/tr>\n<tr>\n<td>Greater than or equal to<\/td>\n<td><span class=\"code\">&gt;=<\/span><\/td>\n<td><span class=\"code\">(x &gt;= 3)<\/span><\/td>\n<td>Numeric or alphabetical comparison.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<div class=\"series-article\">\n<h2>Create JavaScript Structures and Objects<\/h2>\n<p>JavaScript allows you to put together code lines to create functions and variables to create arrays. You can put functions and variables together to create objects.<\/p>\n<table>\n<tbody>\n<tr>\n<td><strong>Element<\/strong><\/td>\n<td><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td><span class=\"code\">function fnName(parameters) {<\/span><span class=\"code\"><br \/>\n<\/span><\/p>\n<p><span class=\"code\">\/\/code<\/span><span class=\"code\"> <\/span><\/p>\n<p><span class=\"code\">} \/\/ end function<\/span><\/td>\n<td>Defines a function named <span class=\"code\">fnName<\/span> and<br \/>\nsends it parameters. All code inside function will execute when the<br \/>\nfunction is called.<\/td>\n<\/tr>\n<tr>\n<td><span class=\"code\">var myArray = new Array(&#8220;a&#8221;,<br \/>\n&#8220;b&#8221;, &#8220;c&#8221;);<\/span><\/td>\n<td>Creates an array. Elements can be any type (even mixed<br \/>\ntypes).<\/td>\n<\/tr>\n<tr>\n<td><span class=\"code\">Var myJSON = {<\/span><span class=\"code\"> <\/span><\/p>\n<p><span class=\"code\">&#8220;name&#8221;:<br \/>\n&#8220;Andy&#8221;,<\/span><span class=\"code\"> <\/span><\/p>\n<p><span class=\"code\">&#8220;title&#8221;:<br \/>\n&#8220;Author&#8221;<\/span><span class=\"code\"> <\/span><\/p>\n<p><span class=\"code\">}<\/span><\/td>\n<td>Creates a <span class=\"code\">JSON<\/span> object. Each element<br \/>\nhas a name\/value pair, and can contain anything, including an array<br \/>\n(with square braces) another <span class=\"code\">JSON<\/span> object,<br \/>\nor a function.<\/td>\n<\/tr>\n<tr>\n<td><span class=\"code\">Var person = new Object();<\/span><span class=\"code\"> <\/span><\/p>\n<p><span class=\"code\">Person.name =<br \/>\n&#8220;Andy&#8221;;<\/span><\/td>\n<td>Creates an object. You can add ordinary variables (which become<br \/>\nproperties) or functions (which become methods).<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<div class=\"series-article\">\n<h2>Change Your Web Page with JavaScript Document Object Model Methods<\/h2>\n<p>The Document Object Model methods shown in the following table offer you a great way to access and modify your Web pages through your JavaScript code.<\/p>\n<table>\n<tbody>\n<tr>\n<td><strong>Element<\/strong><\/td>\n<td><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td><span class=\"code\">myElement =<br \/>\ndocument.getElementById(&#8220;name&#8221;);<\/span><\/td>\n<td>Gets an element from the page with the specified ID and copies<br \/>\na reference to that element to the variable <span class=\"code\">myElement<\/span>.<\/td>\n<\/tr>\n<tr>\n<td><span class=\"code\">myElement.innerHTML =<br \/>\n&#8220;value&#8221;<\/span><\/td>\n<td>Changes the value of the element to <span class=\"code\">&#8220;value&#8221;<\/span>.<\/td>\n<\/tr>\n<tr>\n<td><span class=\"code\">document.onkeydown = keyListener<\/span><\/td>\n<td>When a key is pressed, a function called <span class=\"code\">keyListener<\/span> is automatically activated.<\/td>\n<\/tr>\n<tr>\n<td><span class=\"code\">document.onmousemove =<br \/>\nmouseListener<\/span><\/td>\n<td>When the mouse is moved, a function called <span class=\"code\">mouseListener<\/span> is automatically activated.<\/td>\n<\/tr>\n<tr>\n<td><span class=\"code\">setInterval(function, ms);<\/span><\/td>\n<td>Runs function each <span class=\"code\">ms<\/span><br \/>\nmilliseconds.<\/td>\n<\/tr>\n<tr>\n<td><span class=\"code\">myArray =<br \/>\ndocument.getElementByName(&#8220;name&#8221;)<\/span><\/td>\n<td>Returns an array of objects with the current name (frequently<br \/>\nused with radio buttons).<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>JavaScript Variable Manipulation Functions As shown in the following table, you can use these JavaScript statements in your own code to create and modify variables in your JavaScript functions. Element Description var myVar = 0; Creates a variable with given starting value. Type is determined dynamically. stringVar = prompt(&#8220;message&#8221;) Sends message to user in a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[27],"tags":[],"class_list":["post-707","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/posts\/707","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/comments?post=707"}],"version-history":[{"count":14,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/posts\/707\/revisions"}],"predecessor-version":[{"id":831,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/posts\/707\/revisions\/831"}],"wp:attachment":[{"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/media?parent=707"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/categories?post=707"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/tags?post=707"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}