Javascript Flashcards
What is the HTML format
<html><head>
</head>
<body> ...
<main>
</main>
</body>
</html>
is it a group practise to declare variables before you used them
True
Can the console change objects in the HTML browser
True
what do you use to create comments in JS
// for asingle line,
/*
*/ for paragraphs
What is short cut for commenting out a line of code for debugging purposes
Place the cursor on a line and type crtl /
What is short cut for commenting out multiple lines of code for debugging purposes
highlight the lines and type ctrl /
what characters is added to the end of lines of code
;
What does it mean when we say JavaScript is an “object-oriented” language?
JavaScript is modeled around objects with properties and methods which can be handled in a modular fashion.
What happens to the website and the code when you write code in the browser console?
Code in the browser console impacts the current browser instance only. It exists in the console for as long as the window is open.
What is an indicator of someone being a good JavaScript developer?
They follow standards, invest in learning, use formatting and linting tools for consistency, and write accessible code.
What is the natural environment for JavaScript?
The browser, server environments, and your computer.
What is ECMAScript?
The specification describing how browsers should implement and interpret JavaScript.
Where should you develop and test your JavaScript code?
Develop in a code editor, test in as many browsers as you can get your hands on.
Why have command line and automation tools become popular in JavaScript development?
They simplify complex processes and introduce features to help developers write better code.
How do you indicate to the browser that a section of a program is JS
Is it a good practice to place JS code at the end of a doc
False
What is antipattern
Loading JS at the footer
What is JS modules
Pieces of code save in files and importing/exporting them when they are needed. The code is saved under Script.JS file.
When does the browser execute JavaScript?
By default: When the script is encountered. If the script is set to “async”, when the script is fully loaded. If the script is set to “defer”, when the entire HTML page is rendered.
What happens when you defer JavaScript?
The browser loads the JavaScript asynchronously when it is encountered, then waits until all HTML is rendered before executing the script.
JavaScript modules are heavily used in frameworks like React and Vue. What is the advantage of using modules?
Modules enable modularization of code where individual functions, components, data objects, and other parts can be separated into individual files.
What is the correct markup for adding an external JavaScript file to an HTML document?
what do properties do in JS
Describe objects
What are methods
Properties changing features inside objects
What is the structure of constants
const backpack1 = {
name : “Everyday backpack”,
volume : 30,
lidOpen : “false”,
straplength : {
left: 26,
right : 26,
toggleLid: function(lidStatus) {
this.LidOpen = lidStatus;
},
newStrapLength: function (lengthleft, LengthRight) { this.straplength.left = lengthLeft; this.straplength.right = lengthRight; }, } ,
}
What is the meaning of the keyword this?
Keyword refers to the current object
Cant we change objects inside a container
false
what is the structure of the properties
key: value pairs such as name: everyday backpack
How to display the property pocket Number of backpack object
console.log(“ The pocket value:”, backpack.PocketNum ); or
console.log(“ The pocket value:”, backpack”[PocketNum]” ); this is more in control.
How to access an inner property of the object straplength
console.log(“Strap lenght L:”, backpack.strapLength.left );
When a function is inside an object is also known as
Method
What is the most easy syntax for defining a method of function expression.
ToggleLid : function (LidStatus) {
this.LidOpen = LidStatus;
};
What is non method function syntax for defining a method.
255
What is the structure for creating a class
Class Declaration –> class Name { constructor (//Define parameters 1 .. n) { //Define properties this.name = name; .. this.n = “n”}} or
Class Expression –> const Name = class { }
Define a class definition of Backpack - Class Declaration
class Backpack {
constructor(
// Defines parameters:
name,
volume,
color,
pocketNum,
strapLengthL,
strapLengthR,
lidOpen
) {
// Define properties:
this.name = name;
this.volume = volume;
this.color = color;
this.pocketNum = pocketNum;
this.strapLength = {
left: strapLengthL,
right: strapLengthR,
};
Provide an example of Class Expression - using the class Backpack
import Backpack from “./Backpack.js”;
const everydayPack = new Backpack(
“Everyday Backpack”,
30,
“grey”,
15,
26,
26,
false
);
console.log(“The everydayPack object:”, everydayPack);
console.log(“The pocketNum value:”, everydayPack.pocketNum);
The values match the definition of parameters.
At what point a class can be used in the code
Only after has been declared.
Where in the code we should do import of files
at the top of the code.
Are classes preferred over the constructed functions
True
Given the code below, how do you access the property named in let propName?
let propName = “color”
const myObject = {
ID = 3,<br></br>
color = “pink”,
propLength = 4,
use = false
};
Using bracket notation:
myObject[propName]
In the following object, what is the code in the second line called?
const myObject = {
color: “pink”
};
An object property with a property name and a property value.
Why is the best-practice to place objects inside constants?
So the object isn’t accidentally altered or overwritten.
Which of the below object property names are not valid?
const myObject = {
propName = “property”, // line 1
prop-name = “hyphenated”, // line 2
3rdProp = “numbered”, // line 3
$prop = “dollar”, // line 4
%prop = “percentage”, // line 5
prop name = “space” // line 6
};
Lines 2, 3, 5, and 6
How do you access an object in JavaScript?
No: If the class is a constant, this will cause an error. If the class is not a constant, the new object will overwrite the class.
How do you define an object in JavaScript?
Create a variable, give it a name, and assign it an object using curly brackets:
const myObject = {
// Properties and methods go here.
};
What does the this keyword refer to in a class?
this refers to the current object created from the class.
Where do you go to find official documentation and code examples for standard built in (global) objects?
the MDN Web Docs for standard built-in objects
What is one advantage to using a class over an object constructor method?
Classes can be extended.
What is the established convention for formatting objects?
All properties and methods are listed on their own separate line.
What is the difference between a function and a method?
A function is a stand-alone function. A method is a function within an object.
Can you use arrow functions to create object methods?
No, object methods must be declared using function expressions or the method definition shorthand.
What is a template literal
is is an strategy to to utilise JavaScript to manipulate HTML when rendiring a document on the browser.
When creating a class, the prototype methods are added inside the constructor method.
False
What does the back ticks indicate to the browser
Any code inside is template literal meaning it is a mixture of HTML, JavaScript and Strings.
what does the browser do when reading an HTML file
It creates a document model. This document can be modified with JavaScript.
What is DOM
Document Object Model
what is the basic structure expression of if
if (variable - test){instructions of what to do}
What is the name of the content inside the function () Parenthesis
Arguments which are bits of data they need to do their job.
What does the break statement do
It breaks the current loop.
What is the basic structure of swtich
const food = “sushi”;
switch (food) {
case “sushi”:
console.log(“Sushi is originally from Japan.”);
break;
case “pizza”:
console.log(“Pizza is originally from Italy.”);
break;
default:
console.log(“I have never heard of that dish.”);
break;
}
What is the syntax of class
class name {
// class body
}
class name extends otherName {
// class body
}
Describe the syntax of a function used to create the area of an square.
function calcRectArea(width, height) {
return width * height;
}
console.log(calcRectArea(5, 6));
// Expected output: 30
Can a variable declare by ““let be change during execution
True
What is “let’” declaration
The let declaration declares re-assignable, block-scoped local variables, optionally initializing each to a value.
What does const declared
The const declaration declares block-scoped local variables.
Can variables defined inside a function be accessible outside the function
False
What are the two types of scope
Local and Global scope
What are global variables
Variables declared outside the block.
What are local variables
They are declared inside a block.
What are the two special names for parameters
Default and REST parameters.
Why there is var keyword used in some programs
The var keyword is used in pre-ES6 versions of JS.
Is Let the preferred way to declare a variable
True
Variables that have not been initialized store the primitive data type undefined.
True
In ES6, template literals use backticks ` and ${} to interpolate values into a string.
True
What does the semicolon indicate to the code flow
Semi-colon tells the interpreter to stop parsing and begin compiling. An if statement does not end until after the else block.
What is another way to indicate is equal to
Is equal to: ===
What is another way to indicate is not equal to
Is not equal to: !==
what is this comparison indicator indicates ===
3 equals signs is known as Identity / strict equality, meaning that the data types also must be equal.
When is == used for
When using double equals in JavaScript we are testing for loose equality. Double equals also performs type coercion. Type coercion means that two values are compared only after attempting to convert them into a common type.
What is the Boolean outcome of this statement: false === 0
// false (Different type and different value)
What is the Boolean outcome of this statement: false == 0
// true; It has to do with falsy values in JavaScript. it’s because in JavaScript 0 is a falsy value.
Type coercion will actually convert our zero into a false boolean, then false is equal to false.
What are the 6 falsy values in JavaScript
false — boolean false
0 — number zero
“” — empty string
null
undefined
NaN — Not A Number
What are consider to be ‘rules’ of falsy values
false, 0, and “” for example:
false == 0
// true
0 == “”
// true
“” == false
// true
How can I test if variable sale is true
if(sale) {} or if (sale === true) {}
What is the syntax for ternary operators
Variable ? expression 1
: expression 2
What is the syntax for SWITCH Keyword
Variable
switch (variable) {
case variable1 content :
Statement1
case variable n content :
Statement n
}
What is the syntax for declaring functions
function keyword identifier () {statement};
function greetWorld ()
{
console.log(Hello, world’)
}
What is a hoisting feature
access to function declarations before they’re defined.
What is the syntax for calling a function
function name()
What is the name of the input variables defined in the () of the function
Parameters
What is the name of the input values pass to the function ()
Arguments
Arguments can be passed to the function as
values or variables
When can we define default parameters
When there is an opportunity to be sure the parameters has a value.
When a function is called, the computer will run through the function’s code and evaluate the result. By default, the resulting value is undefined.
True.
What are helper functions
Function inside another function
What is the syntax for function expression
const identifier = function keyword(parameters) {
statements
}
What is call a function with no name
Anonymous function
Can function declaration be hoisted
False
What is implicit return
A function body composed of a single-line block does not need curly braces. Without the curly braces, whatever that line evaluates will be automatically returned. The contents of the block should immediately follow the arrow => and the return keyword can be removed.
What is scope
Scope defines where variables can be accessed or referenced.
Where are variables declared in terms of global scope
Outside the blocks.
What is block scope
When a variable is defined inside a block, it is only accessible to the code within the curly braces {}.