JavaScript Flashcards
scripts
programs that accompany web pages.
Run in web browsers and make web pages more dynamic and powerful
variables
should start with lower case letter
displays in browser console
console.log()
displays on the web page
document.write()
mark the end of a statement
semicolon
optional unless we write multiple statements in a single line
comments
// single line /*multi line*/
open the console
Ctrl + Shift + J
javascript
programming language of the web
create a variable
var name/identifier = value;
undefined
a value
maximum number of decimal places JavaScript can handle
17
to know what type of data a variable contains
console.log(typeof(variable’s name));
data types
string
numbers
Boolean
object
special kind of comparison operator that requires the value and the type to be equal
===
logical and
logical or
&&
||
assign a value to a variable based on a condition being true or false
conditional operator
?
if non-boolean values have real value except 0, “”, undefined
when converted condition returns true
perform different actions based on different condition
conditional statements
if, switch
concatenate strings
string.concat(string);
to find how many characters a string have
string.length;
escaping character
\
extract a piece of string
slice(start, end);
substr(start, length);
replace
string.replace(search string, replace string);
change a string to use lower/upper case letters
string. toLowerCase();
string. toUpperCase():
multiplication and devision with a non-numeric string result in
NaN
not a number
but its type is number
casting
parstInt(string);
parseFloat(string);
round a number
Math.round(number);
Math.random();
produces a random number between 0 and 1
Date
date.getFullYear();
date.getMonth();
date.getDate();
date.getDay(); returns week day as a number from 0 to 6
toDateString();
special kind of variable that contains several values of a type or even different types
array
great way to store related values
var array_name = [content];
[ ]
array litteral
turns an array into a comma-separated string
array. toString();
array. join();
a property that keeps track of number of values in the array
length
create arrays from strings
string.split(separator);
add values at the end of an array
array.push(value);
removes the last value from array
array.pop();
remove a value from the beginning of an array
array.shift();
add a value at the beginning of an array
array.unshift(value);
can use with multiple values
a method that can add, insert, replace and remove values
array.splice(index, how many values we want to remove, optional value to be added at the index);
sort an array
in alphabetical order - array.sort();
reverse order - array.sort().reverse();
for loop
for(control_variable; condition; increment){statement;}
enhanced for loop
suppose thing is an array
for (var thing in things){……}
do while loop
runs at least one, even if the condition is false
skip specific iteration but continue loop
continue
find if a word ends with something
string.endsWith(“text”);
sample function definition
function sayHello(){console.log(“Hello!”);}
variables that contain several values or properties
objects
object literal
{ }
sample object
var person = {name:”Kajan”, age:22, height:1.62};
if we want to get rid of a property
delete object.key;
defining a method
speak : function(){………}
use ……….. keyword to create as many instances of the object as we want
new
javascript can be placed in the …………. and the …………. sections of an HTML page
body
head
javascript display possibilites
writing into an alert box
writing into an HTML output
writing into an HTML element
writing into the browser console
writing into an alert box
window.alert()
writing into an HTML output
document.write()
writing into an HTML element
using innerHTML
writing into the browser console
console.log()
javascript increases the speed by
validating user inputs before sending to the server and also reducing the load on the server
JavaScript code is written inside ………… of the html file
you need to set the ………… attribute of the script to ……………
script tag
type
“text/javascript”
variables in javascript are declared using …….. keyword
var
in Javascript it is not required to specify the type of value a variable is going to hold
javascript itself takes care of it
variable name should start with
alphabet or underscore
cannot begin with numbers
javascript variables are ……………..
case sensitive
scope of javascript variable can be …………. or ………….
local
global
a website might be just static content
web app would have dynamic content
the way javascript sees its containing pages’ data
DOM - Document Object Model
render
to draw
display a message box with an OK button
alert()
display a message box which prompts for some user input
prompt(text, default_text)
display a dialog box with a message, ok and cancel button
confirm()
returns true if the user clicked ok
return true if the value and type of two variables are not equal
!==
conditional or ternary operators
checks a condition and based on the condition evaluates the value of a variable
?
bitwise and(&)
perform boolean AND operation on each bit of its variable
bitwise or
|
bitwise xor
bitwise not
~
left shift(<>)
to shift all bits in the first operand to left by the number of bits in the right operand
check whether a particular property is present within an object
in operator
check whether an object is an instance of the specified object
instanceof