JavaScript Flashcards
Which command syntax available in Php isn’t supported by Javascript ?
(single line command) is not supported by JavaScript
How to define a variable ? How is it different from Php ??
var variableName;
In PHP, variable name starts with $.
Create an array and point out the difference with PHP array ?
var myVariable = [1, “Bob”,”Stev”,10 ]; var names= new Array("Tony", "Mike", "Dan"); // load array by putting values in round brackets alert("Second element of array is: "+names[1]); // index 1 will get 2nd element in array
In PHP, $fruits = array(“Apple”, “Grapes”);
String concatenation
x= “The answer is “ + 42 //The answer is 42
In PHP, “The answer is “. 42
querySelector()
Returns the first match of the <h1> tag and stores result in a Variable</h1>
querySelectorAll()
Returns all matches of the <h1> tags and stores result in myVariable </h1>
getElementsByTagName()
document.getElementsByTagName(‘p’);
Will get all the <p> tags in the HTML page </p>
getElementById()
document.getElementById(custName)
Locates the element whose ID name is custName
parseInt()
- Parses a string argument
- Extracts integer from start of string
- Ignores leading whitespaces
parstInt() and radix examples
parseInt(“F”, 16); // treats it as base 16 and returns 15
parseInt(“1111”, 2); // treats it as base 2 and returns 15
parseInt(“546”, 2); // returns NaN as digits of string are not valid for binary
parseFloat()
similar to parseInt, but returns a floating point number
Number()
converts numeric strings to numbers – integer or float. Ignores leading whitespaces
Number(“12”);// returns 12
Number(“12.3”);// returns 12.3
Number(“12px”);// returns NaN due to present of non
-numeric values in the string
Math.max()
Math.min()
Returns the largest of a given set of numbers
Returns the smallest of a given set of numbers
Math.round()
Returns the value of a number, rounded to the nearest integer
Math.round( 20.49); // returns 20
Math.round(20.5); // returns 21
alert vs prompt
alert is just to display the msg
prompt is two-way ie. to give msg and get answer from user
var num1=prompt(“enter first num “);
var num2=prompt(“enter second num”);
var ans=num1*num2;
alert(“The answer on multiplying your numbers is “+ans);