1
00:00:00,632 --> 00:00:05,032
of the getName function in person1. Next,

2
00:00:05,032 --> 00:00:08,232
we look at some of the built-in JavaScript

3
00:00:08,232 --> 00:00:09,757
functions. JavaScript has several

4
00:00:09,757 --> 00:00:12,076
functions built into the language. In this

5
00:00:12,076 --> 00:00:14,432
module, we'll look at what they are and

6
00:00:14,432 --> 00:00:20,132
how to use them. We begin with the eval

7
00:00:20,132 --> 00:00:21,632
function. Eval, as its name suggests,

8
00:00:21,632 --> 00:00:23,965
accepts a string as input, evaluates it,

9
00:00:23,965 --> 00:00:27,489
and returns a value. We have two

10
00:00:27,489 --> 00:00:30,132
variables, x and y, initialized to the

11
00:00:30,132 --> 00:00:33,299
values 1 and 2 respectively. Next, we

12
00:00:33,299 --> 00:00:35,965
print the output of the eval function that

13
00:00:35,965 --> 00:00:38,299
takes an expression in string format as

14
00:00:38,299 --> 00:00:43,007
input. The value 4 is printed to the

15
00:00:43,007 --> 00:00:45,432
console. In this example, we declare an

16
00:00:45,432 --> 00:00:48,132
additional string variable s, with the

17
00:00:48,132 --> 00:00:51,632
value 'abc'. If you try to evaluate the

18
00:00:51,632 --> 00:00:55,299
expression 'x + y + s', the string value

19
00:00:55,299 --> 00:00:58,632
3abc is displayed in the console.

20
00:00:58,632 --> 00:01:01,132
Therefore, when JavaScript comes across a

21
00:01:01,132 --> 00:01:04,132
string value 'abc', it concatenates that

22
00:01:04,132 --> 00:01:06,965
value with the number 3 and returns the

23
00:01:06,965 --> 00:01:08,965
string value. The function parseInt parses

24
00:01:08,965 --> 00:01:11,632
a string and returns an integer.

25
00:01:11,632 --> 00:01:13,882
Optionally, you can also specify an

26
00:01:13,882 --> 00:01:16,632
additional base or radix argument to

27
00:01:16,632 --> 00:01:20,232
return the integer of that base. If the

28
00:01:20,232 --> 00:01:22,332
first argument cannot be converted to a

29
00:01:22,332 --> 00:01:24,507
number, then parseInt returns a special

30
00:01:24,507 --> 00:01:29,632
value of NaN, not a number. Here, both the

31
00:01:29,632 --> 00:01:31,932
statements print 15 to the console. The

32
00:01:31,932 --> 00:01:34,032
first statement returns the value 'F' in

33
00:01:34,032 --> 00:01:35,965
base 16, while the second statement

34
00:01:35,965 --> 00:01:38,965
returns the value of '15' in decimal,

35
00:01:38,965 --> 00:01:42,757
which is also 15. The third statement

36
00:01:42,757 --> 00:01:45,832
returns a not a number. ParseFloat works

37
00:01:45,832 --> 00:01:49,346
just like parseInt, but it returns a

38
00:01:49,346 --> 00:01:51,832
floating point number. The first two

39
00:01:51,832 --> 00:01:55,032
statements here return 3.99 and 39.9

40
00:01:55,032 --> 00:01:57,032
respectively, and we get not a number for

41
00:01:57,032 --> 00:01:58,775
the third statement as an empty string

42
00:01:58,775 --> 00:02:01,232
cannot be parsed to a floating point

43
00:02:01,232 --> 00:02:03,632
number. Escape returns the hexadecimal

44
00:02:03,632 --> 00:02:07,632
encoding of an argument in the ISO Latin-1

45
00:02:07,632 --> 00:02:10,132
character set. For example, alphanumeric

46
00:02:10,132 --> 00:02:13,632
characters like 'text' is text in this

47
00:02:13,632 --> 00:02:15,965
character set, while non-alphanumeric

48
00:02:15,965 --> 00:02:18,632
characters are represented as percent xx,

49
00:02:18,632 --> 00:02:22,632
where xx is a hexadecimal encoding of the

50
00:02:22,632 --> 00:02:25,632
ASCII character in the ISO Latin-1

51
00:02:25,632 --> 00:02:27,965
character set. Unescape does the opposite

52
00:02:27,965 --> 00:02:32,232
of escape. It returns the ASCII string for

53
00:02:32,232 --> 00:02:35,918
a given input value. Let's show these

54
00:02:35,918 --> 00:02:39,232
functions in a demo. This is our eval

55
00:02:39,232 --> 00:02:41,632
function. If we save the file, the

56
00:02:41,632 --> 00:02:45,268
expression 'x + y + 1' is evaluated first,

57
00:02:45,268 --> 00:02:49,932
and then 4 is printed to the console. When

58
00:02:49,932 --> 00:02:52,032
we declare another variable s with the

59
00:02:52,032 --> 00:02:55,299
value 'abc' and pass the string 'x + y +

60
00:02:55,299 --> 00:02:58,132
s' to the console, the numbers are added

61
00:02:58,132 --> 00:03:00,632
first and then concatenated with the

62
00:03:00,632 --> 00:03:04,832
string 'abc', printing '3abc' to the

63
00:03:04,832 --> 00:03:07,632
console. Here, we are printing the output

64
00:03:07,632 --> 00:03:10,132
of three separate calls to parseInt. Each

65
00:03:10,132 --> 00:03:12,882
parseInt function takes two arguments, a

66
00:03:12,882 --> 00:03:15,132
string and the base the value needs to be

67
00:03:15,132 --> 00:03:17,814
parsed to. If only the value is provided,

68
00:03:17,814 --> 00:03:20,832
the decimal base is assumed, so we get not

69
00:03:20,832 --> 00:03:24,632
a number for the value "F" and 15 for the

70
00:03:24,632 --> 00:03:27,132
value "15". Again, we are printing the

71
00:03:27,132 --> 00:03:29,299
output of three separate calls to

72
00:03:29,299 --> 00:03:33,132
parseFloat here to the console. And if the

73
00:03:33,132 --> 00:03:35,854
input is invalid, the output in the

74
00:03:35,854 --> 00:03:39,382
console is not a number. Finally, we are

75
00:03:39,382 --> 00:03:41,299
making three calls to escape, followed by

76
00:03:41,299 --> 00:03:44,132
three calls to unescape, and the output is

77
00:03:44,132 --> 00:03:46,382
displayed in the console. We use the

78
00:03:46,382 --> 00:03:48,775
output from the escape statements as the

79
00:03:48,775 --> 00:03:53,000
input for the unescape statements. This shows us how encoding and decoding works.

