Recent Posts

JS Cheatsheet

   


BASICS: 

Including JavaScript in an HTML page 

<script type = "text/javascript>js code here </script>

Call an External JavaScript File 

 <script src="myscript.js"></script>

Including comments 

//simngle line comoments 

/*multi line comments*/


VARIABLES: (var,const,let)

var

Can be reassigned but only accessed within the function 

const 

Cannot be reassigned and not accessible before they appear within the code 

let 

Similar to const, however, it can be reassigned but not re-declared.


DATA TYPES:

numbers 

var age = 20 

text(string)

var a ="sid"

boolean 

var b = true

constant numbers 

const pi=3.14

object 

var name -{
       firstName:"sid" 

       lastName:"pandey"

}


ARRAYS:

var fruit = ["banana" , "apple" , "pear"]

methods:

concat() - for concating the arrays 

indexOf() - retures the position of element in array 

join() - combine elements into single strings 

lastindex() - gives last elemet position 

pop()  - removes the last element 

push() - add new element at end 

reverse() - revere the oder of the element 

shift() - remove the first element of an array 

slice()  - pulls a copy of a portion of an array 

sort()  - sorts the elements alphabetically

splice() - add elements in a specified way and position 

toString() - converts elemets into strings 

unshift() - adds a new elemets to the beginning 

valaueOf() - retuns the primitive value of the specified object


FUNCTIONS:

function name(parameter 1, parameter 2,...){

            what the function does 

}

alert()

output data in an alert box in the browser window 

confirm()

opens up ayes/no dialog and returns true/false depending on user click 

console.log()

writes info to the browser console, good for debugging purposes

document.write()

write to the HTML document 

prompt()

creates a dialogue for the user's input 


LOOPS:

for()

for (befor loop; conditon for loop;exectue after loop){

    write what to do during the loop

}

while()

while(condition for loopo)[

    write ehat to do during the loop 

    reinitialization

}

do while()

initialization 

do(){

        write ehat to do during the loop 

        reinitialization 

} while (condition for loop)


No comments

If you have any doubts, Please let me know