Variables & Constants in JavaScript (OCR A Level Computer Science)

Revision Note

Becci Peters

Expertise

Computer Science

Variables & Constants in JavaScript

Variables and constants are used to store a single item of data in a program. This can be accessed through the identifier. Variables can be changed during program execution while constants remain the same.

Declaring variables & constants

  • Variables are declared using a data type, a name and a value (optional)

  • Constants are declared using the 'const' keyword, a name and a value

  • In all programming languages, variable names should follow certain rules, such as:

    • Starting with a letter

    • Not containing spaces

    • Can contain letters, numbers, _ or $

    • Not using reserved words (like if, while, for etc.)

  • Examples of data types include integer, float, boolean, and string

Examples in pseudocode:

Declare a variable called 'score' with a value of 10

score = 10

Declare a constant called 'PI' with a value of 3.14

const PI = 3.14

Examples in JavaScript:

let score = 10;

const PI = 3.14;

Exam Tip

  • You might see code that declares variables like this

var variableName = value;

  • This is bad practice as it utilises global variables but was used until 2015 when let was added to JavaScript

You've read 0 of your 0 free revision notes

Get unlimited access

to absolutely everything:

  • Downloadable PDFs
  • Unlimited Revision Notes
  • Topic Questions
  • Past Papers
  • Model Answers
  • Videos (Maths and Science)

Join the 100,000+ Students that ❤️ Save My Exams

the (exam) results speak for themselves:

Did this page help you?

Becci Peters

Author: Becci Peters

Becci has been a passionate Computing teacher for over 9 years, teaching Computing across the UK helping to engage, interest and develop confidence in the subject at all levels. Working as a Head of Department and then as an educational consultant, Becci has advised schools in England, where her role was to support and coach teachers to improve Computing teaching for all. Becci is also a senior examiner for multiple exam boards covering GCSE & A-level. She has worked as a lecturer at a university, lecturing trainee teachers for Computing.