1/125
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
Why don't you want to use document.write in production code?
If you use it after an HTML document is loaded, it will delete all existing HTML
What is a computer program?
A list of instructions to be executed by a computer. In a programming language, these programming instructions are called statements.
What do JavaScript statements consist of?
Values, Operators, Expressions, Keywords and Comments
Which character set does JS use?
Unicode, which covers almost all of the characters, punctuations and symbols in the world
How do you create multiline comments in JS?
Multi-line comments start with / and end with /
What are variables in JS?
They are containers for storing data values
What are JS identifiers?
All JavaScript variables must be identified with unique names. These unique names are called identifiers. They are case-sensitive.
When defining a number for a variable value, do you have to use quotes?
No
What is it called when you create a variable in JS?
Declaring a variable
How do you assign a value to a variable that has already been declared?
carName = "Volvo";
Where should you declare your variables in JS?
At the beginning of the script.
Can you define multiple variables in a single statement?
var person = "John Doe", carName = "Volvo", price = 200;
This can also span multiple lines:
var person = "John Doe",
carName = "Volvo",
price = 200;
Does a variable still have the same value if you redeclare it without a new value?
Yes
What are the JS assignment operators?
=
+=
-=
*=
/=
%=
Can the += operator be used with strings?
var txt1 = "What a very ";
txt1 += "nice day";
What are the JS Comparison Operators
== equal to
=== equal value and equal type
!= not equal
!== not equal value or not equal type
> greater than
< less than
>= greater than or equal to
<= less than or equal to
? ternary operator
What are the JS Logical Operators?
&& logical and
|| logical or
! logical not
What are the JS Type Operators?
typeof Returns the type of a variable
instanceof Returns true if an object is an instance of an object type
What are the JS Arithmetic Operators?
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus (Remainder)
++ Increment
-- Decrement
What are the numbers in an arithmetic operation called?
Operands. The operation (to be performed between the two operands) is defined by an operator
How do you declare variables with scientific (exponential) notation?
var y = 123e5;
var z = 123e-5;
How do you declare a JS array?
JS arrays are written with square brackets and the array items are separated by commas. Array indexes are zero-based, so the first item is [0].
How do you write JavaScript objects?
In-between curly braces. Object properties are written as name:value pairs, separated by commas.
var person = {firstName:"John", lasName:"Doe"};
What does the typeof operator return for these two examples?
typeof ""
typeof 3.41
"string"
"number"
What happens if you set a variable to undefined?
car = undefined;
The value is set to undefined and the typeof will also be undefined
In JS, what is null?
Null is supposed to be nothing, but it is actually an Object
What is a primitive data value?
It is a single simple data value with no additional properties and methods.
What primitive data types can the typeof operator return?
string
number
boolean
undefined
What complex data types can the typeof operator return?
function & object. The typeof operator returns object for objects, arrays and null. The typeof operator does not return object for functions, it returns "function". The typeof operator returns "object" for arrays because in JavaScript arrays are objects.
How is a JavaScript function defined?
It is defined with the function keyword, followed by a name, followed by parenthesis (). In the parenthesis you can include parameter names, separated by commas. The code executed by the function is placed inside curly brackets.
What are function arguments?
They are the values received by the function when it is invoked. Inside the function, the arguments behave as local variables.
What statement will stop a function from executing?
When JavaScript reaches a return statement, the function will stop executing. Functions often compute a return value. The return value is "returned" back to the "caller"
What makes functions useful in JS?
You can reuse code: Define the code once and use it many times. You can use the same code many times with different arguments, to produce different results.
What difference does the () make in regards to Functions in JS?
in the function toCelcius, the toCelcius refers to the function object, and toCelsius() refers to the function result. Accessing a function without () will return the function definition instead of the function result?
How can functions be used in JS?
They can be used the same way you use variables, in all types of formulas, assignments and calculations.
How do local variables work?
Variables declared within a JavaScript function become local to the function, meaning they can only be accessed from within the function.
Are objects variables in JS?
Yes, but objects can contain many values.
What are objects in JS?
JavaScript objects are containers for named values called properties or methods.
How do you create an object in JS?
You define (and create) a JavaScript object with an object literal.
What are the name:value pairs in JS called?
properties
How can you access object properties?
objectName.propertyName or
objectName["propertyName"]
Along with properties, what else can objects have? And what are they? How are they stored?
Objects can also have methods, which are actions that can be performed on objects. They are stored in properties as function definitions
What is an example of a method stored in an object?

What is the 'this' keyword in JS?
In a function definition, this refers to the "owner" of the function. In an example, this is the person object that "owns" the fullName function. In other words, this.firstName means the firstName property of this object.
How do you access an object method?
objectName.methodName()
What is an HTML event and what do they consist of?
HTML events are "things" that happen to HTML elements. When JavaScript is used in HTML pages, JS can "react" on these events.
An HTML event can be something the browser does, or something a user does. HTML allows event handler attributes, with JavaScript code, to be added to HTML elements.
Examples: An HTML web page has finished loading, An HTML input field was changed. An HTML button was clicked.
What is an example of a Javascript event handler attribute being added to an HTML element? Though it is more common to see event attributes calling functions.
In JS, how do you find how many characters are in a string?
use the .length property
What should you do if you need to have quotation marks in your string?
Escape those characters with \" or \' or \\
What are the 9 valid escape sequences in JS?
\"
\'
\\
\b Backspace
\f Form Feed
\n New Line
\r Carriage Return
\t Horizontal Tabulator
\v Vertical Tabulator
For readability, how long should each line of code be?
80 characters
To keep under the line length limit where should you break the statement?
After an operator if possible
how do you convert a variable's value to lower case?
txt.toLowerCase();
how do you convert a variable's value to upper case?
txt.toUpperCase();
How do you concatenate two strings stored in variables str1 and str2?
var result = str1.concat(str2);
How do you find the position of the first occurrence of a specified text in a string?
str.indexOf("locate"); or str.search("locate");
How do you find the index of the last occurance of a specified text in a string?
str.lastIndexOf("locate");
What do indexOf() and lastIndexOf() return if the text is not found?
-1
What can the second (optional) parameter in indexOf() and lastIndexOf() do for you?
The second parameter is the starting position for the search
What's the difference between search() and indexOf()?
indexOf() is for plain substrings, search() can do regular expressions. And the search() method cannot take a second start position argument.
What are the three methods for extracting a part of a string?
slice(start, end)
substring(start, end)
substr(start, length)
What does the slice() method do?
slice() extracts a part of a string and returns the extracted part in a new string.
The method takes 2 parameters: the starting index (position), and the ending index (position).
What happens when you enter a negative parameter in slice()?
If a parameter is negative, the position is counted from the end of the string.
var str = "Apple, Banana, Kiwi";
var res = str.slice(-12, -6);
What happens if you omit the second parameter using slice?
The method will slice out the rest of the string, starting from the end of the string if a negative value is used.
var str = "Apple, Banana, Kiwi";
var res = str.slice(-12)
What's the difference between slice() and substring()?
substring() cannot accept negative indexes
What's the difference between slice() and substr()?
For substr(), the second parameter specifies the length of the extracted part. And like slice, omitting the second parameter, it will slice out the rest of the string.
What method would you use to replace a specified value with another value in a string?
replace("This string", "With this string");
How does the replace() method work?
By default, it only replaces the first match it finds, and is case sensitive
How do you replace a string using a case insensitive search?
use a regular expression with an /i flag (insensitive)
var n = str.replace(/MICROsoFT/i, "W3Schools");
How do you replace all incidents of a string?
use a regular expression with a /g flag (global match)
var n = str.replace(/Microsoft/g, "W3Schools");
How would you globally replace all versions in a case insensitive way?
var n = str.replace(/MiCroSOFt/gi, "W3Schools");
Are strings immutable?
Yes, strings cannot be changed, only replaced. All string methods return a new string, they don't modify the original string.
How do you remove whitespace from both sides of a string.
str.trim();
What methods are there to extract string characters?
charAt(position)
charCodeAt(position)
property access [] (don't use)
How would you return the character at a specified index (position) in a string?
str.charAt(0);
How would you return the unicode of the character at a specified index in a string? Which unicode does it use?
str.charCodeAt(0);
UTF-16 code (integer between 0 and 65535)
How do you convert a String to an Array?
split(","); to split on commas
How do you write extra large or extra small numbers using scientific (exponent) notation?
var x = 123e5; //12300000
var y = 123e-5; //0.00123
How does JavaScript define numbers?
There are no different types of numbers, like integers, short, floating, ect. They are always stored as double precision floating point numbers, following the International IEEE 754 standard. This format stores numbers in 64 bits, where the number (the fraction) is stored in bits 0 to 51, the exponent in bits 52 to 62 and the sign in bit 63.
How many digits are integers accurate up to?
15
var x = 999999999999999; // x will be 999999999999999
var y = 9999999999999999; // y will be 10000000000000000
What is the maximum number of decimals in JS? What is a problem with decimals in JS? How can you remedy the issue?
The maximum number of decimals is 17, but floating point arithmetic is not always 100% accurate
var x = 0.2 + 0.1; // x will be 0.30000000000000004
To solve the problem above, it helps to multiply and divide:
var x = (0.2 10 + 0.1 10) / 10; // x will be 0.3
Which direction does the JavaScript compiler work? How is this relevant to adding numbers or concatenating numbers and strings?
left to right, so below, 10 and 20 are added, and then that result is concatenated with the string "30"
var x = 10;
var y = 20;
var z = "30";
var result = x + y + z; // result = 3030
What is the result of dividing a number by a string that can't be converted into a number?
NaN (not a number)
How can you find out if something is not a number?
isNaN(x); //returns true because x is not a number
What typeof is NaN?
"number"
When does JavaScript return Infinity or -Infinity?
If you create a number outside the largest possible number
var x = 2 / 0; // x will be Infinity
var y = -2 / 0; // y will be -Infinity
What typeof is Infinity?
"number"
Using a function in JS, how would you show that this is equal to a random object that you create?

What does a function expression vs a function declaration look like?
function myFunctionDeclaration() {}
var myFunctionExpression = function() {};
What is var x = 0xFF; equal to?
x will be 255
JavaScript interpretes numeric constants as hexadecimal if they are preceded by 0x
How many unique characters can you create with two Hex characters? What are a couple examples?
256 characters
0x00 = 0
0x11 = 17
0x0A = 11
Why don't you want to write a JS number with a leading 0 (like 07)?
Because some JS versions interpret numbers as Octal if they are written with a leading zero
What does the number method .toString() do for you with regards to number base?
It will output numbers from base 2 to base 36.
var myNumber = 32;
myNumber.toString(10) = Decimal - 32
myNumber.toString(16) = Hexadecimal - 20
myNumber.toString(8) = Octal - 40
myNumber.toString(2) = Binary - 100000
Why don't you want to create your own number objects?
Normally, JS numbers are primitive values created from literals. var x = 123
Using var y = new Number(123); slows down execution speed, complicates the code and can produce some unexpected results, like the fact that you can't compare the 'objects' (x == y) will be false.
Can numbers like 3.14 and 2014 have properties and methods.
No, because they are not Objects. However, methods and properties are also available to primitive values, because JavaScript treats primitive values as objects when executing methods and properties
How would you convert a number to a string?
nmbr.toString();
How would you convert var x = 9.656; to 9.66e+0 and 9.6560e+0
x.toExponential(2);
x.toExponential(4);
What does the toFixed(); method do?
It returns a string, with the number written with a specified number of decimals
var x = 9.656;
x.toFixed(2); // returns 10
x.toFixedd(6); // returns 9.656000
What does the toPrecision(); method do?
It returns a string, with a number written with a specified length.
var x = 9.656;
x.toPrecision(); // returns 9.656
x.toPrecision(2); // returns 9.7
x.toPrecision(4); // returns 9.656
x.toPrecision(6); // returns 9.65600
What are the methods that convert variables to numbers?
The Number() method // Returns a number, converted from its argument
The parseInt() method // Parses its argument and returns a floating point number
The parseFloat() method // Parses its argument and returns an integer
These methods are not number methods, but global JS methods