This animated series will cover essential principles that a JavaScript developer needs to know.

Challenge 1 – Data Types

JavaScript has seven data types.

  • 6 primitive data types:
    • Number
    • String
    • Boolean
    • Symbol
    • Null
    • Undefined
  • and Object

Note: Function and Array are special kind of objects.

In this series, you’ll learn more about Number, String, Boolean, Undefined and Object types. Read about Symbol and Null on MDN – Data Structures.

Can you guess what are the data types involved in the following code example?

{a: 42, b: true};

'bar', {} and { a: 42, b: true } are called literal values: they are not stored in a variable.

Challenge 2 – Declare global variables, initialize with primitive values

There are three kinds of declarations in JavaScript:

  • var
  • let
  • const

We’ll focus on the var statement. Learn more about let and const on MDN – Grammar and types.

var declares a variable, optionally initialized to a value.

In the following code sample, do you know what values alice and bob hold? And, by the way, what holds the alice and bob variables themselves?

var alice;
var bob = 42;

A variable declared outside of any function is called a global variable. In a web page, global variables declared with the var statement are properties of the global object window. This may be different in other contexts, like Node.js or web workers. In this series, the context is a web page.

Challenge 3 – Initialize a variable with an object

Do you know what value carol holds?

var carol = {a: true};

This is not as obvious as it may seem:

You have learned:

  • Primitive values (Number, String, Boolean…) are held directly by variables
  • Non-primitive values (objects) are held by references, not by variables

Challenge 4 – Assign primitive values

Can you guess what the last line returns?

alice = 'foo';
bob = alice;
alice = 'bar';

Primitive values are always assigned by value-copy. In bob = alice;, bob is assigned a copy of alice‘s value: the 'foo' value.

In JavaScript, you can’t have a variable alice pointing to another variable bob.

Next week, you’ll learn more about objects. If you have any remarks or questions about this series, don’t hesitate to add a comment below or send me a message @CedricSoulas. Follow us on @Wakandasoft to catch the next update.