Javascript Flashcards
Javascript
https://developer.mozilla.org/en-US/docs/Web/JavaScript
alert( )
Pop up message to give user an alert.
Data Type
Don’t have to specify anything specifically (most of the time). We can force it to recognize certain characters are numbers though.
Data types include “string”, number, boolean.
You can check data type using typeOf( ‘variable here’ )
Variable and Constants.
Variables are declared with ‘var’ and ‘let’
Constants are declared with ‘const’.
‘let’ is preferred in most situation while declaring a variable.
Variable naming
Please use Camel Casing.
String concatenate
Just use ‘+’ sign between your strings.
String length
string.length;
Slicing and Extracting parts of String
string.slice( x , y )
x - the start of slicing, the string starting from zero (always keep in mind)
y - index of character upto which we should cut, not including that particular character.
Example - let a = “Koolaid”;
a.slice(4, 7) - Output —-> aid
Arithematic
+, -, *, /, %
% - remainder
++ increament
– decreament
Math.round( ) ————> Generate random number from 0 to 0.99
Math.floor( ) —————> Rounds down a number to nearest lower integer
Function call and Return
function functionName( (argument) ) { code here; return some-variable-here; }
Random Number
Random number between 1 and 5
Math.floor( Math.random( ) * 5 + 1 );
math.random( ) * 5 —–> generates a number from 0 to 4.99
Conditional statements and Comparators
If{ } else { }
comparators are ‘ < ‘ , ‘ > ‘, ‘ === ‘, ‘ <= ‘, ‘ >= ‘, ‘ !== ‘
’ == ‘ compares but doesnt comapre data type, and ‘ === ‘ compares the value as well as data types.
Combine conditions using ‘ && ‘, ‘ || ‘, ‘ ! ‘ (not).
Array
a= [1, 2, 3, 4, 5, 6];
a=[ ];
a[2] = 3;
because, arrays and strings start index from zero, 0.
Looping
while ( condition -> true ) { Loops code executes; }
for ( var initial, var conditional, var increment ) { Loop code here; }