You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

3.4 KiB

Anything to Declare ?

Variables

Values, need a way to be identified, that's why we use variables.
They add meaning to a value by pointing to it.

It's like a label, a way to name things.

If we say 20, it doesn't carry much meaning, 20 what ?

Imagine we are talking about what's in your backpack, you have 20 pairs of socks.

Now that's a lot of socks !

We defined what we have (pair of socks) and it's value (20)

Example

An identifier is used to define what it is, using this syntax:

let pairOfSocks = 20

😱 Woa what's all this ?!

Let's explain each parts:

Keyword: let

Firstly, a keyword, let.

A keyword is a special word that JS knows about, it is used to tell the computer to perform a specific action.

let that indicate the script that it's defining a new variable.

Identifier

After that, it need a valid identifier.

In this case it's pairOfSocks, we chose what ever we want here that will be meaningful, (it's often hard to name things correctly).

A few rules to apply to make sure an identifier is valid:

  • No space allowed (pair of socks would be 3 distinct identifiers)
  • Not starting with a number (that's reserved for number values)
  • Not being a reserved keyword (for example using let)
  • No special characters

As such we use what's called camelCase.

Note that in JS, it is a convention to not uppercase the first letter as this is reserved for special declarations, we won't go into details now.

let pair of socks = 20 // invalid because of spaces
let 'pair of socks' = 20 // invalid because identifiers are not strings
let pair-of-socks = 20 // invalid because of special character -
let pair.of.socks = 20 // invalid because of special character /
let 20PairOfSocks = 20 // invalid because beginning with a number
let PairOfSocks = 20 // valid but incorrect because of the uppercase
let pairOfSocks = 20 // Just right
let let = true // invalid because `let` is a JS keyword

Operator: =

The special character = is an operator, like in math, they are used to define specific operations.

In this case, = define the assignation operation.

It means assigning a value to our variable.

This is what links the choosen identifier with our value.

Value

Lastly, a value, like the one you already know: string, number and boolean.

Full example with descriptive comments:

// ↙ keyword        ↙ assignation operator
let comicBookTitle = 'Tintin in Tibet'
//       ↖ identifier       ↖ the value (here a string)

Using multiple variables to define something more complex:

// Example of variables that could represent a transaction:
let currency = 'EURO'
let amount = 77.5
let cashPayment = false

// Use them with console.log, like a normal value:
console.log('You have to pay:')
console.log(amount)
console.log('in')
console.log(currency)
console.log('using cash:')
console.log(cashPayment)

Instructions

All right, before we can embark into this adventure, you are going to tell us more about yourself using variables !

  • Declare a variable indentified age of a Number value of your age

  • Declare a variable indentified name of a String value of your name

  • Declare a variable indentified secureLuggage of a Boolean saying if your luggage contain dangerous things or not. (for obvious security reasons)

PS: And please, use reasonable values, or the tests might reject you !