Javascript Language

CSE 121b

Week 2: The Fundamentals

Some Similarities and Differences

Shared Operators

Remember, both Python and JavaScript have C as one of their ancestors. Because of this, it is not shocking to find out that they share many of the same operators as C. These include +, -, /, *, ==, !=, <, >, etc. By going to these two sites, you can compare the operators for the two languages. Python's list of operators to JavaScript's list of operators. When you do so, you will find that Python has a few that JavaScript does not. You will also see that the conditional operators, also known as logical operators, exist in both languages but use different characters.

JavaScript's missing operators

Because Python and JavaScript are different languages and the creators of JavaScript had different opinions than those who created Python, there are some Python operators you should not expect to find in JavaScript. They are:

Don't get the wrong idea. Each of these behaviors can be done in JavaScript, they will just be done a little differently. Take a look at Table 1 to see how.

Table 1: C Equivalents of Python Operators
Python JavaScript
a//b Math.trunc(a/b)
a <> b a != b
a**=b a = Math.pow(a,b)
a and b a && b
a or b a || b
not(a and b) !(a && b)

JavaScript has other operators available as well.

Variables

In Python, a variable can hold any type of value and can change the type of value it holds. For example, this code is valid in Python.

age = 3
age = 5
age = 4.2
age = 'old'

JavaScript variables behave in much the same way. Just like in Python, they can hold any type and the type can change. However, JavaScript does require you to use the let keyword to declare a variable. Like this:

let age = 3
age = 5
age = 4.2
age = 'old'

Constants

In Python, Constants are declared using an all upper case name

PI=3.14

In JavaScript this is done differently, instead of using all upper case names, JavaScript requires the use of the const keyword. Like this:

const PI = 3.14

While it is not required that the names of your constants be all upper case, it is considered good form.

For more information about declaring and using variables in Javascript please go now and read You Don't Know JS Yet, Ch2 through the section Declaring and Using Variables. And then read MDN: Javascript Basics through the section Data structures and types.

Wrap Up

As with all things, there are pluses and minuses of requiring keywords like const and let and having or not having some operators. One disadvantage of requiring let and const is the extra cluttering of the code with 'unnecessary' words. An advantage is explicitness. Everyone that later reads your code will know exactly what you meant to have happen. Also, because of this explicitness, a variable can't be accidentally declared twice in JavaScript, a situation which can produce strange, difficult to debug, bad behavior in large Python code sets.

Which operator set to use in a language often depends on the languages. Perhaps the creators of Python thought the syntax of Structured Query Language (SQL) was easier to understand and so they adopted its and, or, and not operators. This is opinion and as such it and the alternative C operators used in JavaScript, &&, ||, !, are just as valid. Don't get caught up in the psuedo-religious ferver that some programmers adopt. Every language is good at some things and bad at others. This includes both Python and JavaScript.

Arrays

In JavaScript, arrays can be declared like this.


let names = ['Bob','Sue','Jorge','Svetlana']
	

This should look very familiar to you.

You access elements of an array like this


let aName = names[0]
	

with zero being the index of the first element in the array. Modifying values in the array is doable also.


names[2]='George'
	

Now the third element of the array is George instead of Jorge.

JavaScript has a lot of Built in Functions(BIF's), or built-in object methods, that you can use to modify arrays. Imagine you needed to use an array to keep track of customers lined up to see a movie. As people show up, they need to line up in the order they arrived. You can simulate this using the push method of array.

If Bob, Sue, George, and Svetlana were already waiting in line for the show in that order, and then Grace showed up too, you would use push to add Grace at the end of the array (the back of the line).


names.push('Grace')
	

Print the updated names array to the console and you see Grace added on the end.


console.log(names)
//Displays ['Bob','Sue','George','Svetlana','Grace']
	

Since you can add elements to the end of the array, surely there is a way to remove elements from the end of the array. You do this with the pop method.


names.pop()
console.log(names)
//Displays ['Bob','Sue','George','Svetlana']
	

Can you remove elements from the end or add them to the beginning of the array? You bet. There are a large number of methods that you can apply to array. Check them out. Also, you'll be learning about using filter, sort, map, and reduce array methods next week.