W3Schools JavaScript

0.0(0)
studied byStudied by 1 person
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/125

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

126 Terms

1
New cards

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

2
New cards

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.

3
New cards

What do JavaScript statements consist of?

Values, Operators, Expressions, Keywords and Comments

4
New cards

Which character set does JS use?

Unicode, which covers almost all of the characters, punctuations and symbols in the world

5
New cards

How do you create multiline comments in JS?

Multi-line comments start with / and end with /

6
New cards

What are variables in JS?

They are containers for storing data values

7
New cards

What are JS identifiers?

All JavaScript variables must be identified with unique names. These unique names are called identifiers. They are case-sensitive.

8
New cards

When defining a number for a variable value, do you have to use quotes?

No

9
New cards

What is it called when you create a variable in JS?

Declaring a variable

10
New cards

How do you assign a value to a variable that has already been declared?

carName = "Volvo";

11
New cards

Where should you declare your variables in JS?

At the beginning of the script.

12
New cards

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;

13
New cards

Does a variable still have the same value if you redeclare it without a new value?

Yes

14
New cards

What are the JS assignment operators?

=

+=

-=

*=

/=

%=

15
New cards

Can the += operator be used with strings?

var txt1 = "What a very ";

txt1 += "nice day";

16
New cards

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

17
New cards

What are the JS Logical Operators?

&& logical and

|| logical or

! logical not

18
New cards

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

19
New cards

What are the JS Arithmetic Operators?

+ Addition

- Subtraction

* Multiplication

/ Division

% Modulus (Remainder)

++ Increment

-- Decrement

20
New cards

What are the numbers in an arithmetic operation called?

Operands. The operation (to be performed between the two operands) is defined by an operator

21
New cards

How do you declare variables with scientific (exponential) notation?

var y = 123e5;

var z = 123e-5;

22
New cards

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].

23
New cards

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"};

24
New cards

What does the typeof operator return for these two examples?

typeof ""

typeof 3.41

"string"

"number"

25
New cards

What happens if you set a variable to undefined?

car = undefined;

The value is set to undefined and the typeof will also be undefined

26
New cards

In JS, what is null?

Null is supposed to be nothing, but it is actually an Object

27
New cards

What is a primitive data value?

It is a single simple data value with no additional properties and methods.

28
New cards

What primitive data types can the typeof operator return?

string

number

boolean

undefined

29
New cards

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.

30
New cards

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.

31
New cards

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.

32
New cards

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"

33
New cards

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.

34
New cards

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?

35
New cards

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.

36
New cards

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.

37
New cards

Are objects variables in JS?

Yes, but objects can contain many values.

38
New cards

What are objects in JS?

JavaScript objects are containers for named values called properties or methods.

39
New cards

How do you create an object in JS?

You define (and create) a JavaScript object with an object literal.

40
New cards

What are the name:value pairs in JS called?

properties

41
New cards

How can you access object properties?

objectName.propertyName or

objectName["propertyName"]

42
New cards

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

43
New cards

What is an example of a method stored in an object?

knowt flashcard image
44
New cards

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.

45
New cards

How do you access an object method?

objectName.methodName()

46
New cards

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.

47
New cards

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.

48
New cards

In JS, how do you find how many characters are in a string?

use the .length property

49
New cards

What should you do if you need to have quotation marks in your string?

Escape those characters with \" or \' or \\

50
New cards

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

51
New cards

For readability, how long should each line of code be?

80 characters

52
New cards

To keep under the line length limit where should you break the statement?

After an operator if possible

53
New cards

how do you convert a variable's value to lower case?

txt.toLowerCase();

54
New cards

how do you convert a variable's value to upper case?

txt.toUpperCase();

55
New cards

How do you concatenate two strings stored in variables str1 and str2?

var result = str1.concat(str2);

56
New cards

How do you find the position of the first occurrence of a specified text in a string?

str.indexOf("locate"); or str.search("locate");

57
New cards

How do you find the index of the last occurance of a specified text in a string?

str.lastIndexOf("locate");

58
New cards

What do indexOf() and lastIndexOf() return if the text is not found?

-1

59
New cards

What can the second (optional) parameter in indexOf() and lastIndexOf() do for you?

The second parameter is the starting position for the search

60
New cards

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.

61
New cards

What are the three methods for extracting a part of a string?

slice(start, end)

substring(start, end)

substr(start, length)

62
New cards

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).

63
New cards

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);

64
New cards

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)

65
New cards

What's the difference between slice() and substring()?

substring() cannot accept negative indexes

66
New cards

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.

67
New cards

What method would you use to replace a specified value with another value in a string?

replace("This string", "With this string");

68
New cards

How does the replace() method work?

By default, it only replaces the first match it finds, and is case sensitive

69
New cards

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");

70
New cards

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");

71
New cards

How would you globally replace all versions in a case insensitive way?

var n = str.replace(/MiCroSOFt/gi, "W3Schools");

72
New cards

Are strings immutable?

Yes, strings cannot be changed, only replaced. All string methods return a new string, they don't modify the original string.

73
New cards

How do you remove whitespace from both sides of a string.

str.trim();

74
New cards

What methods are there to extract string characters?

charAt(position)

charCodeAt(position)

property access [] (don't use)

75
New cards

How would you return the character at a specified index (position) in a string?

str.charAt(0);

76
New cards

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)

77
New cards

How do you convert a String to an Array?

split(","); to split on commas

78
New cards

How do you write extra large or extra small numbers using scientific (exponent) notation?

var x = 123e5; //12300000

var y = 123e-5; //0.00123

79
New cards

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.

80
New cards

How many digits are integers accurate up to?

15

var x = 999999999999999; // x will be 999999999999999

var y = 9999999999999999; // y will be 10000000000000000

81
New cards

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

82
New cards

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

83
New cards

What is the result of dividing a number by a string that can't be converted into a number?

NaN (not a number)

84
New cards

How can you find out if something is not a number?

isNaN(x); //returns true because x is not a number

85
New cards

What typeof is NaN?

"number"

86
New cards

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

87
New cards

What typeof is Infinity?

"number"

88
New cards

Using a function in JS, how would you show that this is equal to a random object that you create?

knowt flashcard image
89
New cards

What does a function expression vs a function declaration look like?

function myFunctionDeclaration() {}

var myFunctionExpression = function() {};

90
New cards

What is var x = 0xFF; equal to?

x will be 255

JavaScript interpretes numeric constants as hexadecimal if they are preceded by 0x

91
New cards

How many unique characters can you create with two Hex characters? What are a couple examples?

256 characters

0x00 = 0

0x11 = 17

0x0A = 11

92
New cards

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

93
New cards

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

94
New cards

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.

95
New cards

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

96
New cards

How would you convert a number to a string?

nmbr.toString();

97
New cards

How would you convert var x = 9.656; to 9.66e+0 and 9.6560e+0

x.toExponential(2);

x.toExponential(4);

98
New cards

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

99
New cards

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

100
New cards

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