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

Revision Note

Becci Peters

Written by: Becci Peters

Reviewed by: James Woodhouse

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;

Examiner Tips and Tricks

  • 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

Last updated:

You've read 0 of your 5 free revision notes this week

Sign up now. It’s free!

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

Expertise: Computer Science

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.

James Woodhouse

Author: James Woodhouse

Expertise: Computer Science

James graduated from the University of Sunderland with a degree in ICT and Computing education. He has over 14 years of experience both teaching and leading in Computer Science, specialising in teaching GCSE and A-level. James has held various leadership roles, including Head of Computer Science and coordinator positions for Key Stage 3 and Key Stage 4. James has a keen interest in networking security and technologies aimed at preventing security breaches.