Javascript Flashcards
What is Javascript
Js is a programming language that is
- lightweight
- cross platform
- object oriented
- one of three core technologies
- commonly used in webpages
Can you use js in the server end?
Yes with node.js
How do you add js to a webpage?
you either add the code in a script tag in the html file
or you create a new js file and add a script source in the html
ex
what is a variable?
A variable is a container in which we can store values
how do you declare variables in js?
var name = ‘Name’;
What are datatypes?
there are different types of values
What kind of datatypes are there in js?
- numbers( always have floats)
- strings
- booleans
- Undefined(datatype that does not have a value yet)
- Null (non existent)
What is dynamic typing?
Dynamic typing means that js understands what kind of datatype you are working with.
How do you comment out code in javascript?
- single line comment //
- multiline comment /* stuff */
What is type coercion?
Type conversion means that javascript will transform the datatype to another by itself
Can we declare a variable without declaring a value to that?
Yes that is possible in javascript, the output will be shown as “undefined”.
What is a variable mutation?
Variable mutation is when we assign variables new values.
What is the difference between arguments and parameters?
Argument is what the user passes in when the function is called.
A parameter is the placeholders for arguments in the function.
How do you call a function in Javascript?
var someThing = function(argument);
How do you write function statements?
function someFun(parameter { //code }
How do you write a function statement?
var someFun = function(parameter) { // code }
What is the difference between a function expression and a function statement?
A function statement performs an action
A function expression produces a value
How do you create an array in js?
var name = ['john', 'jane', 'mark'] or var years = new Array(1990, 1999, 1948)
How do you get an object from an array?
names[0];
index in js are zero based (the start from zero)
What are some array functions?
.push(‘something’); adds an element in the end
.unshift(‘something’); adds an object in the beginning
.pop( ); deletes the last element
.shift( ); deletes the first element
.indexOf(‘something’); shows the index of that element in an array
What are objects in js?
objects are key value pairs
What is a for loop?
the for loop loops through a block of code a number of times.
the for loop is the most used loop in javascript
for (var i = 0; 1 < 20; i++) {
console.log(i);
}
what is the .length method?
the lenght method tells us how many elements there are in an array.
What is a while loop?
The while loop runs a block of code while the condition is true. var i = 0; while( i < names.lenght) { console.log(names[i]); }
What question does scoping answer?
Where can we access a certain variable?
What is “Lexical Scoping”
A function that is lexically within another function gets access to the scope of the outer function. Basically gets access to the variable for the parent scope
What is the scope chain?
The scope chain is in what order you can access the variables in a particular scope.
It only works from inside out.
What is the execution context?
It’s the order in which functions are called
the start from the global and work inwards.
What is the ‘This’ Keyword?
It is a variable that each and every execution context gets.
What is method borrowing?
It’s when you use another function to define a variable instead of creating a new one.
ex
mike.calculateAge = john.calculateAge
Where the function calculateAge is defined inside the john object.
What is the DOM?
it stands for Document Object Model
And is a structured representation of html
What is the DOM used for?
It is used to connect webpages to scripts like javascript
What is the difference between classes and id’s in html?
The classes can be used over and over, but the id’s needs to be uniqe
How can you declare multiple variables in a clean way?
var scores = [1, 2]; var round = 0;
to
var scores, round;
scores = [1, 2];
round = 0;
How do you generate a random number?
you use the Math.random(); function for that.
How do you manipulate the DOM?
document.querySelector(‘class or id’).somecontentMethod = variable
How do you read from the DOM?
You use the getElement, querySelector, elemenById methods
How do you change CSS styles with javascript?
you use the .style and the css method that you want to use ex display = ‘none’
How do you change text in an html element?
you use the textContent method
How do you change text in an html element?
you use the textContent method
How do you change an html element?
You use the innerHtml method
How do you change an html element?
You use the innerHtml method
What are events?
Events are notifications that are sent to notify the code that something has happened on the webpage
How are event triggered?
Events are triggered by clicks, scrolling, pressing keys or resizing windows.
What are event listeners?
Event listeners are functions that perform an action based on a certain event, it waits for a specific event to take place.
How do you set up a event handler?
step 1 select the element in which the event will occur like button class step 2 add the event listner method .addEventListener step 3 add two arguments, the event like click and the function that should start
What is a callback function?
A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action.
What is an anonymous function?
Functions stored in variables do not need function names. They are always invoked (called) using the variable name.
(a function without a name).
What is console.table ?
Console.table prints out a table of an object, making it more readable in the chrome console.
What is console.clear?
Console.clear clears out the chrome console
What is console.err
Console.err prints out an error message in the chrome console.
What is console.war
console.war prints out an warning message in the chrome console.
What is console.time and console.endTime ?
It shows how long it takes for the code between the console.time and console.endTime to execute with a numerical value in the chrome console.
What can you have in an variable?
letters, numbers, _, and $
Variables can not start with a number
What are the two main datatypes in JS?
Primitive Data types and Reference Data typers
What is Primitive Data Types?
They are stored directly in the location the varible accesses (Stored on the Stack)
Boolean. Null. Undefined. Number. String. Symbol (new in ECMAScript 6)
What is Reference Data Types?
They are acessed by reference, Objects that are stored on the heap
A pointer to a location in memory
Arrays
Object literals
Functions
Dates
What is typeof?
typeof shows what kind of datatype a variable stores.
How can you convert a data type to a string in JS?
with the String method
ex:
let val = String(123);
=> ‘555’
or
with the toString method
ex:
let val = (5).toString();
=> ‘5’